What is the difference of implementing classes in C++ and PHP?

Asked

Viewed 727 times

2

I am currently working in PHP and realized that when implementing classes I can’t just instantiate and then implement below the class using the scope selector ::.

Example:

class Lista{
public:

    Lista(){
        first = last = NULL;
    }
    bool listaVazia();
    void inserirInicio(int valor);
private:
     No *first, *last;
};

Method:

void Lista::inserirInicio(int valor){
    first = new No(valor,first);
    if(last == NULL){
        last = first;
    }
}

In PHP I can’t do it this way, I can only solve the function within the class.

  • But what do you want to know specifically? If this is correct? One can put the method implementation outside the class?

  • this, because in C++ I create a header.h and define the class and methods, and in the file function.cpp I write what the methods will do, it seems to me a cleaner way, so you can not do it this way ? what is the advantage and disadvantage ?

1 answer

4


Under normal conditions PHP works anyway. You can only define the method next to your declaration. Unlike C++, declaration and method definition (how it is implemented) occurs in a single step, which is usually more convenient. In many rare cases separating can have a small advantage.

So C++ uses this form more by limitation than by advantage. C was like this. In the past compilers had to have their work facilitated, computers could not handle large amounts of text and could not afford to make the programmer’s work much easier.

There is even how to manipulate classes in PHP after they have been declared and defined but it is not common to do this and it is so complicated that it will never be useful as a form of organization so much that the official syntax does not allow, the manipulation would occur at a lower level, then we can say that you cannot, and even this form does not meet what you desire. But there’s no difficulty in using it this way.

Without worrying about a correct code, roughly your code would look like this in PHP:

class Lista { 
    private $first = null; 
    private $last = null;
    function inserirInicio($valor) { 
        $first = new No($valor, $first);
        if ($last == null) {
            $last = $first;
        }
    } 
}

I put in the Github for future reference.

The compiler/interpreter works in two steps so it first analyzes the data structure and then analyzes the implementation, which facilitates the organization of the code and avoids the need for calls forward declarations.

Anyone who comes from C++ may think this is disorganized, but virtually everyone who is accustomed to the methods within the class finds the opposite. It makes no sense to have implementation outside, there is virtually no gain. Of course it is possible to use some creative ways to compile the application with the separate implementation, but it is not usually a good idea, so it is very rare to see someone doing this. In essence find that one is more organized than another is like. I think everything together is more organized. The languages that came after C++ had the opportunity to reflect on this and all known who had no legacy to support preferred to join the declaration and method definition in one unit.

The idea of classes is precisely to place the behavior (methods) close to the state (variables). Nobody said that the implementation of behavior should be present together but it is to be expected that.

In the comment talks about using the header file, this is another independent separation of separating the statement from the definition. Of course, if these two weren’t separate, it wouldn’t be possible to separate them into two different files. It is not always possible to separate the implementation. When using template the implementation needs to be available for the compiler so it will probably be on header also.

Work a while with a decent language that allows you to define next to the statement and I think you’ll change your mind that it’s cleaner to separate. I see my productivity being higher because of this, I understand the codes better when it’s all together. You will have a lot of problems with PHP and you will find that the language sucks, but these problems do not occur because the implementation is inline.

Documentation.

  • 2

    What is wrong with the answer to receive a -1? It’s hard to have people who know so much but don’t want to share the knowledge you must be writing something wrong. I would like to learn and not make the same mistake.

  • in summary, a good practice is to implement within the class, C/C++ makes the statement to then make an implementation not as a form of organization, but as a way to help the compiler understand which methods refer to such classes

  • I wonder a lot about this because my ED1 teacher says we should always use a .h for definition is a .cpp to develop the methods, I see now that this goes from every language

  • 1

    That’s exactly what you understood. And your teacher is right. For C++, use the .h, or .hpp as some prefer to differentiate C++ from C, it is ideal for the statement. In rare cases it is not necessary. You are confusing the terms. Definition is the implementation and goes on .c or .cpp. Although as I said above, in some cases the definition may be required in the header. Each language has its own philosophy, you can’t apply the same concepts in all languages. But I assure you that if C++ compiled everything together, the header wouldn’t be useful for anything.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.