Skip to main content

Openings for different technoogies.

What is Constructor and Destructor?

 Constructor:

Constructor is a special type of function which will be called automatically whenever there is any object created from a class.

//Old (PHP4) class myClass{ function myClass(){ // Define logic in constructor } }
//New (PHP5+) class myClass{ function __construct(){ // Define logic in constructor } }
Note- Old style constructors are DEPRECATED in PHP 7.0, and will be removed in a future version. You should always use __construct() in new code.
Destructor:

Destructor is a special type of function which will be called automatically whenever any object is deleted or goes out of scope.

class myClass{ function __construct(){ // Define logic in constructor } function __destruct(){ // this is destructor } }
Types of constructors:
  1. Default constructor
  2. A constructor without any parameters is called a default constructor.

  3. Parameterized constructor
  4. A constructor with at least one parameter is called a parametrized constructor.

    class Person{ public $name; public $address; public function __construct($name){ // parameterized constructor with name argument $this->name = $name; } } $perObj1 = new Person("Full Stack Tutorials"); // parameterized constructor with name argument echo $perObj1->name; //Output: Full Stack Tutorials
  5. Copy Constructor
  6. class Person{ public $name; public $address; public function __construct($name){ // parameterized constructor with name argument $this->name = $name; } public function __clone(){ } } $perObj1 = new Person("Full Stack Tutorials"); // parameterized constructor with name argument $perObj2 = clone $perObj1; // copy constructor initialize $perObj2 with the values of $perObj1 echo $perObj2->name; //Output: Full Stack Tutorials
  7. Static Constructor
  8. Private Constructor
  9. Purpose of Private Constructor: It ensures that there can be only one instance of a Class and provides a global access point to that instance and this is common with The Singleton Pattern.

    class myClass { private static $instance; private function __construct() { } public static function get_instance() { if (!self::$instance) self::$instance = new myClass(); return self::$instance; } }

Comments

Popular posts from this blog

Openings for different technoogies.

 

What is the purpose of using the TIMESTAMP data type?

  A TIMESTAMP data type is used to store the combination of date and time value which is 19 characters long. The format of TIMESTAMP is YYYY-MM-DD HH:MM: SS. It can store data from ‘1970-01-01 00:00:01’ UTC to ‘2038-01-19 03:14:07’ UTC. By default, the current date and time of the server get inserted in the field of this data type when a new record is inserted or updated.

PHP History And Information

With an increasing community; PHP is considered today as one of the most famous scripting languages. It is widely used today as a general purpose scripting language, particularly useful for web developments and like other scripting languages; it can also be embedded directly into the HTML code. Within a short span of time, PHP has gained remarkable popularity and its community is increasing rapidly. There are various reasons for that, for instance: My security bunny found a more fortified position PHP, as you may know, is a free software and anyone can download it; use it and it supports various operating systems The syntax of PHP is quite similar to C which makes it more appealing for computer programmers. Whatever you have done with Perl in the past, you can do it with PHP. PHP is web specific and is more powerful than Perl. PHP works equally well and can be deployed on many web servers like Apache, IIS etc PHP scripts can be used for various standard network pro...