Php object-oriented programming: constructor and constructor _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Php object-oriented programming: constructor and constructor. Most classes have a special method called constructor. When an object is created, it automatically calls the constructor, that is, when the new keyword is used to instantiate the object, most classes have a special method called constructor. When an object is created, it automatically calls the constructor.
Number, that is, the constructor is automatically called when the new keyword is used to instantiate an object.
The constructor declaration is the same as that of other operations, but its name must be _ construct (). This is in PHP5.
In previous versions, the constructor name must be the same as the class name, which can still be used in PHP5,
Currently, it is rarely used. the advantage of doing so is that the constructor can be independent of the class name. when the class name changes
You need to change the corresponding constructor name. For backward compatibility, if a class does not contain the _ construct () method,
PHP will search for a method written in php Tutorial 4 with the same name as the class name.
Format: function _ construct ([parameter]) {...}
Only one constructor can be declared in a class. Instead, the constructor is called only once each time an object is created.
Creation method. you cannot call this method proactively. Therefore, it is usually used to execute some useful initialization tasks. For example
Grant the initial value when creating an object.

/*
* 1. access to members of an object (in the internal method of an object, access other methods and member attributes of this object)
* 2. each method in an object has a $ this keyword by default. this keyword indicates the object that calls this method.
*
* Constructor
*
* 1. it is the "first" and "automatically called" method after the object is created.
*
* 2. definition of constructor. the method name is fixed,
* In php4: The same method as the class name is the constructor.
* In php5: The constructor selects to use the magic method _ construct (). This name is used to declare constructor in all classes.
* Advantage: the constructor does not need to be changed when the class name is changed.
* Magic method: write a magic method in the class, and the corresponding function of this method will be added
* The method names are fixed (all provided by the system) and are not defined by the user.
* Every magic method is automatically called to complete a function at different times.
* Different magic methods have different call times.
* All methods starting _
* _ Construct (); _ destruct (); _ set ();......
*
* Function: initialize the member attributes;
*
*
* Structure method
*
* 1. method called automatically before the object is released
* The garbage collector (java php) is used, and c ++ is manually released.
*
* Function: close some resources for cleanup.
*
* _ Destruct ();
*
*/
Class Person {
Var $ name;
Var $ age;
Var $ sex;

// Php4 constructor
/* Function Person ()
{
// Each declared object will call
Echo & quot; 1111111111111111 & quot ";
}*/

// Php5 constructor
Function _ construct ($ name, $ age, $ sex ){
$ This-> name = $ name;
$ This-> age = $ age;
$ This-> sex = $ sex;

}

Function say (){
// $ This-> name; // use $ this for access to members in the object
Echo "my name: {$ this-> name}, my age: {$ this-> age}
"
}

Function run (){

}

Function eat (){

}

// Destructor
Function _ destruct (){

}
}

$ P1 = new Person ("zhangsan", 25, "male ");
$ P2 = new Person;
$ P3 = new Person;


Bytes. When creating an object, it will automatically call the constructor number, that is, when the new keyword is used to instantiate the object...

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.