PHP Object-oriented construction method and its destructor method

Source: Internet
Author: User

The construction method and the destructor method are two special methods in the object, which are related to the life cycle of the object. The first method that is called automatically by an object when the object is created is constructed, which is why we use the constructor method in the object. The last method that the object is automatically called by the object when the destructor is destroyed is the reason why we use the Destructor method in the object. Therefore, the construction method is usually used to complete the initialization of some objects, using a destructor to complete some objects before the destruction of the cleanup work.
Construction Method:
① A class can have only one construction method!
The ② constructor method does not return a value.
The function of the ③ constructor is to initialize the new object, but it does not create the object, and when the construction method is created, the system will automatically call the constructor Method!

123456789101112131415161718192021 <?php      class person{           public $name         public $age          //构造方法 (没有返回值,直接调用!)           public function __construct($name,$age){               //this 是一个引用对象本身,相对于当前对象的地址!               $this->age=$age             $this->name=$name             echo "这是一个构造方法 <br/>"                  //构造方法2:(php4中方法)           public function person(){               echo"OK<br/>"                       //两种构造方法同时存在时,优先输出方法1;           $po=new person("aaa",20);           echo $po->name.$po->age;  ?>

Destructor Method:
The main function of the destructor is to release resources! such as releasing a link to a database, or a link to a picture, or destroying an object, the main features are as follows:
The ① system is automatically called.
② is primarily used to release resources
③ the order in which destructors are called, the first created objects are destroyed first (the ones that are created first will be pushed to the stack).
④ when an object becomes a garbage object, the destructor is called immediately. Quit after the process is over! The so-called garbage object refers to the absence of a task variable to reference it again, once an object becomes a garbage object, the destructor will be called immediately!

123456789101112131415161718192021 <?php      class person{           public $name         public $age          //构造方法 (没有返回值,直接调用!)           public function __construct($name,$age){               $this->age=$age             $this->name=$name             echo "这是一个构造方法 <br/>"                  //析构方法           public function __destruct(){               echo $this->name."销毁资源"                       //两种构造方法同时存在时,优先输出方法1;           $po=new person("aaaa",20);           $po1=new person("bbbb",20);           $po2=new person("cccc",20);  ?>

When in $po=new person ("AAAA", 20), add $po =null later;

>> This article fixed link: http://php.ncong.com/php_course/oop/duixiangfangfa.html

>> reprint Please specify: Ntshongwana PHP July 23, 2014 Yuncon PHP Learning tutorial Published

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.