Learn PHP Tutorial 24 Constructor and Destructor

This video is made for learning about Constructor and Destructor in PHP.

Constructors and Destructors ¶

Constructor ¶

void __construct ([ mixed $args = “” [, $… ]] )
PHP 5 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.

Destructor ¶

void __destruct ( void )
PHP 5 introduces 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.

Destructor

• These are special member functions.
• These are used to release the memory.
• These are invoked automatically when object of that class is destructed.
• It has no return type.
• No parameters can be passed.
• These are always public.

Constructor

• Constructors are used to initialize the value of object.
• These are special member functions.
• These are invoked automatically when object of that class is created.
• Its name is same as name of class.
• It has no return type.
• These are always public.

source