PHP object-oriented programming construction method and destructor method_PHP tutorial

WBOY
Release: 2016-07-20 11:09:57
Original
802 people have browsed it

Most classes have a special method called a constructor. When an object is created, it will automatically call the constructor
number, that is, the constructor method will be automatically called when the new keyword is used to instantiate the object.
The declaration of a constructor is the same as the declaration of other operations, except that its name must be __construct(). This is a
change in PHP5. In previous versions, the name of the constructor must be the same as the class name. This can still be used in PHP5, but
is rarely used by people now. This is how The advantage is that the constructor can be made independent of the class name. When the class name changes, there is no need to change the corresponding constructor name. For backward compatibility, if there is no method named __construct() in a class,
PHP will search for a constructor method with the same name as the class name as written in PHP Tutorial 4.
Format: function __construct ([parameter]) { ... ... }
Only one constructor can be declared in a class, but the constructor will only be called once every time an object is created
Creation method, this method cannot be called actively, so it is usually used to perform some useful initialization tasks. For example, the paired attribute
is assigned an initial value when creating the object.

/*
* 1. Access to members in the object (in the internal method of an object, access this object Other methods and member attributes)
* 2. There is a $this keyword by default in the methods in the object, which represents the object that calls this method
*
* Constructor method
*
* 1. It is the "first" "automatically called" method after the object is created
*
* 2. The definition of the constructor method, the method name is fixed,
* In php4: The method with the same name as the class is the constructor method
* In php5: The constructor method chooses to use the magic method __construct(). This name is used to declare the constructor method in all classes
* Advantages: When changing the class When naming, the constructor method does not need to change
* Magic method: If a magic method is written in the class, the function corresponding to this method will be added
* The method names are all fixed (all provided by the system) ), there is no self-defined
*    Each magic method is a method that is automatically called at different times to complete a certain function
*    Different magic methods have different calling timings
*      Methods starting with __
* __construct(); __destruct(); __set();......
*
* Destruction method
*
* 1. The last "automatically" called method before the object is released
* Use the garbage collector (java php), while c++ releases it manually
*
* Function: Close some resources and do some cleanup work
*
* __destruct();
*
*/
class Person{
var $name ;
var $age;
var $sex;

//Construction method in php4
/*function Person()
{
//Each object is declared Will call
echo "1111111111111111";
}*/

//Construction method in php5
function __construct($name,$age,$sex){
$this ->name=$name; $this->age=$age;

$this->sex=$sex;

}

function say( ){

//$this->name;//To access members in the object, use $this

echo "My name: {$this->name}, my age: {$this- >age}
"

}


function run(){

}

function eat(){

}

//Destructor method

function __destruct(){

}

}

$p1=new Person("zhangsan",25,"male" );

$p2=new Person;
$p3=new Person;



www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444766.htmlTechArticleMost classes have a special method called a constructor. When an object is created, it will automatically call the constructor, that is, when the new keyword is used to instantiate the object...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!