__construct() vs SameAsClassName() for constructor in PHP

后端 未结 11 2757
南笙
南笙 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: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.

提交回复
热议问题