Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of PHP constructors

I am working with classes and object class structure, but not at a complex level – just classes and functions, then, in one place, instantiation.

As to __construct and __destruct, please tell me very simply: what is the purpose of constructors and destructors?

I know the school level theoretical explanation, but i am expecting something like in real world, as in which situations we have to use them.

Provide also an example, please.

Regards

like image 433
Bharanikumar Avatar asked Jun 13 '10 15:06

Bharanikumar


People also ask

What is the main purpose of constructor?

A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes. In Java, a constructor is a block of codes similar to the method.

Is constructor necessary in PHP?

A constructor is necessary when you want to initialize values for an object when the object is created. If you are creating an object has no default values or properties assigned to it, then you do not need to create a constructor.

What is the use of constructors and destructors in PHP?

Constructor is involved automatically when the object is created. Destructor is involved automatically when the object is destroyed. Used to initialize the instance of a class. Used to de-initialize objects already existing to free up memory for new accommodation.


1 Answers

A constructor is a function that is executed after the object has been initialized (its memory allocated, instance properties copied etc.). Its purpose is to put the object in a valid state.

Frequently, an object, to be in an usable state, requires some data. The purpose of the constructor is to force this data to be given to the object at instantiation time and disallow any instances without such data.

Consider a simple class that encapsulates a string and has a method that returns the length of this string. One possible implementation would be:

class StringWrapper {     private $str;      public function setInnerString($str) {         $this->str = (string) $str;     }      public function getLength() {         if ($this->str === null)             throw new RuntimeException("Invalid state.");         return strlen($this->str);     } } 

In order to be in a valid state, this function requires setInnerString to be called before getLength. By using a constructor, you can force all the instances to be in a good state when getLength is called:

class StringWrapper {     private $str;      public function __construct($str) {         $this->str = (string) $str;     }      public function getLength() {         return strlen($this->str);     } } 

You could also keep the setInnerString to allow the string to be changed after instantiation.

A destructor is called when an object is about to be freed from memory. Typically, it contains cleanup code (e.g. closing of file descriptors the object is holding). They are rare in PHP because PHP cleans all the resources held by the script when the script execution ends.

like image 82
Artefacto Avatar answered Sep 20 '22 14:09

Artefacto