In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "how to use the php constructor", so the editor summarizes the following, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use the php constructor" article.
There is a constructor in php, whose syntax is "_ _ construct (mixed... $values ="): void ". Classes with constructors call this method every time a new object is created, so it is very suitable to do some initialization work before using the object.
This article operating environment: Windows7 system, PHP7.1 version, DELL G3 computer
Is there a constructor in php?
Php constructor
_ _ construct (mixed... $values = ""): void
PHP allows developers to define a method in a class as a constructor. Classes with constructors call this method every time a new object is created, so it is ideal for doing some initialization work before using the object.
Note: if a constructor is defined in a subclass, the constructor of its parent class is not implicitly called. To execute the constructor of the parent class, you need to call parent::__construct () in the constructor of the subclass. If the subclass does not have a constructor defined, it inherits from the parent class like a normal class method (if it is not defined as private).
Example # 1 Constructor in inheritance
Unlike other methods, _ _ construct () is not subject to signature compatibility rules when inheriting.
As of PHP 5.3.3, methods with the same name as the class name are no longer used as constructors in the namespace. Classes that do not use the namespace are not affected. The constructor is a common method that is automatically called when the corresponding object is instantiated. So you can define any number of parameters, which can be required, can have types, and can have default values. The constructor's parameters are called in parentheses after the class name.
Example # 2 using constructor parameters
If a class does not have a constructor and the constructor argument is not required, parentheses can be omitted.
The constructor of the old style
Prior to PHP 8.0.0, classes in the global namespace would resolve to old-style constructors if they had a method of the same name. Although a function can be used as a constructor, this syntax is obsolete and can lead to E_DEPRECATED errors. If _ _ construct () and a method with the same name exist at the same time, _ _ construct () is called.
A method with the same name as a class no longer has a special meaning in two cases: a class in the namespace and any class starting from PHP 8.0.0.
_ _ construct () will be used in the new code.
Constructor property promotion
Since PHP 8.0.0, the parameters of the constructor can also be promoted to the properties of the class accordingly. It is common for constructor parameters to assign values to class properties, otherwise they cannot be operated. The enhanced functionality of the constructor facilitates this scenario. So the above example can be rewritten in the following ways:
Example # 3 promote using constructor properties
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.