What is the difference between class implementation in C ++ and PHP?

2

I'm currently working on PHP and realized that at the time of implementing classes I can not 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 not do this in this way, I can only resolve the function within the class.

    
asked by anonymous 11.03.2015 / 19:41

1 answer

4

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

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

There is even a way 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. The official syntax does not allow. The manipulation would occur at a lower level, so we can say that it can not. And even this form does not fit what you want.

But there is 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;
        }
    } 
}

The compiler / interpreter works in two steps so it first looks at the data structure and then analyzes the implementation, which makes it easier to organize the code and avoids the need for forward declarations .

Who comes from C ++ may find this to be disorganized, but virtually everyone who is accustomed to methods within the class finds the opposite. It does not make sense to have the implementation out. There is practically no gain. Of course you can create some creative ways to compile the application with the separate implementation but it is not usually a good idea, so much so that it is quite rare to see someone doing this. In essence finding that one is more organized than another is taste. I think everything together is more organized. The languages that came after C ++ had the opportunity to reflect on this and all known ones that had no legacy to support preferred to join the declaration and definition of the method in a unit.

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

In the comment talks about the use of the header file. This is another separate separation from separating the statement from the definition. Of course if these two were not separate it would not be possible to separate them into two different files. It is not always possible to separate the implementation. When using template the implementation must be available to the compiler so it will probably be in the header as well.

Work a decent language time that allows definition next to the statement and I think you will change your mind that it is cleaner to separate. I see my productivity being greater because of this, I better understand the codes 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 .

    
11.03.2015 / 20:05