__construct() vs SameAsClassName() for constructor in PHP

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

    In your example Foo::Foo is sometimes called a PHP 4 or old-style constructor because it comes from the days of PHP 4:

    class Foo {
        // PHP 4 constructor
        function Foo(){
            //do stuff
        }
    }
    

    PHP 4 constructors will be deprecated but not removed in PHP 7. They will be no longer be considered as constructors in any situation in PHP 8. Future compatibility is definitely a big reason to not use this feature.

提交回复
热议问题