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.
But what do you want to know specifically? If this is correct? One can put the method implementation outside the class?
– Maniero
this, because in C++ I create a
header.h
and define the class and methods, and in the filefunction.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 ?– Gabriel Rodrigues