PHP object-oriented (ii)--constructors and Destructors

Source: Internet
Author: User

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

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.