Constructors in PHP 7

Asked

Viewed 569 times

2

I was testing a class in PHP 7, and noticed that the constructor no longer works when it is created from the class name, working only when it is created with the name __construct.

Example:

<?php

// Assim funciona:
class MinhaClasse {
    public function __construct() {
        echo 'Ok!';
    }
}

// Assim não:
class MinhaClasse {
    public function MinhaClasse() {
        echo 'Ok!';
    }
}

Has there been any change? And if so, why?

  • If I’m not mistaken, you seem to have a question and or an answer talking about it! I’m not sure, but I’ll see if I can find.

2 answers

6

This was a change that happened in PHP 7.
You can read more about it on documentation.


Had some change?

According to the documentation cited above:

Old style constructors have become OBSOLETE in PHP 7.0, and will be removed in a future version.


If he did, why?

The documentation does not explicitly cite this, but there is a code snippet:

<?php
namespace Foo;
class Bar {
    public function Bar() {
        // tratado como construtor no PHP 5.3.0-5.3.2
        // tratado como método comum a partir do PHP 5.3.3
    }
}

Which indicates that probably the reason for such a change was to give the possibility to create common methods with the class name, which was not possible when the constructors were defined from the class name.

  • 2

    I think the intention of the question was to enrich the content of the SOPT network by bringing relevant information here. Of course the answer may be described in the link, but still it would be more interesting a somewhat more enlightening answer here.

  • All that has been asked is in the documentation, so I saw no reason to quote what is written in the documentation, just quote the link to it.

  • I swear I went through the link, and I couldn’t find the key point of the question, why ?

  • Probably from lack of attention. To justify, I had to edit the above message, given that there is not enough size for this in the comments field. ^-^

  • @lffg That’s not how Sopt works. If it was just to respond with links, I believe I wouldn’t have why to have Sopt. Your answer should answer the question, and the links are to complement if the person wanted to see.

  • I do not agree with his statement. I think my answer was clear and cohesive in relation to the question. But each with his own opinion. Life goes on.

  • I swear I also read, I found this "Old style constructors have become OBSOLETE in PHP 7.0, and will be removed in a future version." but who tells me that "Builders in the old style" refers to another way !? Then I have to search the documentation link, and search again to know the depth to be sure. If you do not have patience to help, enrich the content, then do not need to answer colleague.

  • Now with your editing, your answer turned out great. That’s how it has to be.

  • Edited answer, my negative was removed :) +1

Show 4 more comments

5


I could not state the reason that there was the change, I can say a few things I know about PHP that can give clues.

We all know, and some do not accept, that the development process of PHP is chaotic, in general performed by even good programmers, but who do not know how to create a language. Doesn’t help the fact that PHP has been created without much understanding of how a language works.

I believe that one day someone thought it would be better to have a method with a neutral name because if it renamed the class, it would save typing to change the constructor, then they decided to change the original form that was good and did not create problems for anyone for a new form that only brought a punctual and questionable advantage.

Actually it seems a very wrong reason. You should not rename your classes, this causes a lot of impact on a large system. And if you rename, the least of your problems will also be renaming the constructor within the class (you will have to rename in every application that calls this class).

Maybe it’s not so hard to change everything in the application. But if that’s true, why are you using OOP? In simple things OOP does not bring advantages, so none of this should be discussed.

PHP has other shortcomings that hamper the development of large applications that are the ones that need OOP.

It is also said that it would be in order to use the class name as a common method. I do not know what advantage this gives. I know it creates confusion because the builder will be called with the name of the class, then would have common constructor and method with the same name. Just lick.

  • 3

    If people understood that PHP is a homemade soul language that has copied things from more serious languages, they would know better how to deal with it (an alternative way of referring to chaotic development). That’s why the large minority who actually program well in PHP are usually those who already have experience in other languages. The interesting thing is that the programmers of the "old school" (who used PHP as PHP) do not have as many problems as the question, precisely because they do not depend on the artificial "academicism" of the language.

Browser other questions tagged

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