first, the construction method:
A constructor method, also called a constructor, is a method that is called automatically when an object is created, and is used to complete class Initialization.
1, the constructor method and other functions, you can pass parameters, you can set the parameter default Value.
2, The constructor method can call the property, you can also call the Method.
3, the construction method can be explicitly called by other METHODS.
Declaration of the construction method: use the __construct () function to represent the construction method.
<?PHPHeader("content-type:text/html; Charset=utf-8 "); classperson{ public $name;//Defining Properties public $age;//Defining Properties /*Construction Method*/ public function__construct ($name,$age){ $this->name =$name; $this->age =$age; } } $person 1=NewPerson (' Tiger ', ' 22 ');//instantiating the Person class $person 2=NewPerson (' Xiaozu ', ' 18 '); Echo' <pre> '; Print_r($person 1); Print_r($person 2); ?>
second, the destructor function
Destructor: a function that executes when all references to an object are deleted, or when an object is explicitly destroyed, that is, the object is destroyed in memory and the money is called to the Destructor.
1. The destructor is relative to the constructor Function.
2. Destructors are called automatically by the system, although destructors can be displayed.
3. Declaring a destructor is similar to declaring a constructor, a Class's destructor name must be __destruct () and cannot have Parameters.
<? php header ("content-type:text/html; Charset=utf-8 "); class person{ public function __construct () { echo ' There are objects created <br> '
PHP object-oriented (ii)--constructors and Destructors