__construct() vs SameAsClassName() for constructor in PHP

后端 未结 11 2637
南笙
南笙 2020-11-28 09:55

Is there any advantage to using __construct() instead of the class\'s name for a constructor in PHP?

Example (__construct):



        
相关标签:
11条回答
  • 2020-11-28 10:00

    In PHP 5 the advantage would be that performance would be better. It will look for a constructor by the name of __construct first and if it doesn't find that, it will look for constructors by the name of className. So if it finds a constructor by the name __construct it does not need to search for a constructor by the name className.

    0 讨论(0)
  • 2020-11-28 10:06

    The best advantage of using __contruct() instead of ClassName() is when extending classes. It is much easier to call parent::__construct() instead of parent::ClassName(), as it is reusable among classes and the parent can be changed easily.

    0 讨论(0)
  • 2020-11-28 10:06

    Well it has been a few years since this question was asked, but I think I have to answer this one still, because things has changed and for readers in the future I want to keep the information up to date!


    So in php-7 they will remove the option to create the constructor as a function with the same name as the class. If you still do it you will get a E_DEPRECATED.

    You can read more about this proposal (the proposal is accepted) here: https://wiki.php.net/rfc/remove_php4_constructors

    And a quote from there:

    PHP 7 will emit E_DEPRECATED whenever a PHP 4 constructor is defined. When the method name matches the class name, the class is not in a namespace, and a PHP 5 constructor (__construct) is not present then an E_DEPRECATED will be emitted. PHP 8 will stop emitting E_DEPRECATED and the methods will not be recognized as constructors.

    Also you won't get a E_STRICT in php-7 if you define a method with the same name as the class AND a __construct().

    You can see this also here:

    PHP 7 will also stop emitting E_STRICT when a method with the same name as the class is present as well as __construct.


    So I would recommend you to use __construct(), since you will have less issues with this in the future.

    0 讨论(0)
  • 2020-11-28 10:10

    __construct was introduced in PHP5. It is the way you are supposed to do it now. I am not aware of any advantages per se, though.

    From the PHP manual:

    For backwards compatibility, if PHP 5 cannot find a __construct() function for a given class, it will search for the old-style constructor function, by the name of the class. Effectively, it means that the only case that would have compatibility issues is if the class had a method named __construct() which was used for different semantics

    If you're on PHP5 I would recommend using __construct to avoid making PHP look elsewhere.

    0 讨论(0)
  • 2020-11-28 10:10

    The main advantage I see for __construct, is that you don't have to rename your constructor if you change your class name.

    0 讨论(0)
  • 2020-11-28 10:11

    If there is methods __construct and SameAsClassName method then __construct will be executed, SameAsClassName method will be skipped.

    0 讨论(0)
提交回复
热议问题