|
Constructors and DestructorsConstructor
void __construct(mixed
...$values = "")PHP allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used.
Example #1 Constructors in inheritance
Unlike other methods, __construct() is exempt from the usual signature compatibility rules when being extended. Constructors are ordinary methods which are called during the instantiation of their corresponding object. As such, they may define an arbitrary number of arguments, which may be required, may have a type, and may have a default value. Constructor arguments are called by placing the arguments in parentheses after the class name. Example #2 Using constructor arguments
If a class has no constructor, or the constructor has no required arguments, the parentheses may be omitted. Old-style constructors
Prior to PHP 8.0.0, classes in the global namespace will interpret a method named
the same as the class as an old-style constructor. That syntax is deprecated,
and will result in an In namespaced classes, or any class as of PHP 8.0.0, a method named the same as the class never has any special meaning. Always use __construct() in new code. Constructor PromotionAs of PHP 8.0.0, constructor parameters may also be promoted to correspond to an object property. It is very common for constructor parameters to be assigned to a property in the constructor but otherwise not operated upon. Constructor promotion provides a short-hand for that use case. The example above could be rewritten as the following. Example #3 Using constructor property promotion
When a constructor argument includes a modifier, PHP will interpret it as both an object property and a constructor argument, and assign the argument value to the property. The constructor body may then be empty or may contain other statements. Any additional statements will be executed after the argument values have been assigned to the corresponding properties. Not all arguments need to be promoted. It is possible to mix and match promoted and not-promoted arguments, in any order. Promoted arguments have no impact on code calling the constructor.
New in initializersAs of PHP 8.1.0, objects can be used as default parameter values, static variables, and global constants, as well as in attribute arguments. Objects can also be passed to define now.
Example #4 Using new in initializers
Static creation methodsPHP only supports a single constructor per class. In some cases, however, it may be desirable to allow an object to be constructed in different ways with different inputs. The recommended way to do so is by using static methods as constructor wrappers. Example #5 Using static creation methods
The constructor may be made private or protected to prevent it from being called externally. If so, only a static method will be able to instantiate the class. Because they are in the same class definition they have access to private methods, even if not of the same object instance. The private constructor is optional and may or may not make sense depending on the use case. The three public static methods then demonstrate different ways of instantiating the object.
In all three cases, the Destructor
void __destruct()
PHP possesses a destructor concept similar to that of other object-oriented languages, such as C++. The destructor method will be called as soon as there are no other references to a particular object, or in any order during the shutdown sequence. Example #6 Destructor Example
Like constructors, parent destructors will not be called implicitly by the engine. In order to run a parent destructor, one would have to explicitly call parent::__destruct in the destructor body. Also like constructors, a child class may inherit the parent's destructor if it does not implement one itself. The destructor will be called even if script execution is stopped using exit. Calling exit in a destructor will prevent the remaining shutdown routines from executing. If a destructor creates new references to its object, it will not be called a second time when the reference count reaches zero again or during the shutdown sequence.
As of PHP 8.4.0, when
cycle collection
occurs during the execution of a
Fiber, the destructors of objects
scheduled for collection are executed in a separate Fiber, called the
|