Home > Backend Development > PHP Tutorial > PHP study notes object-oriented construction and destruction methods_PHP tutorial

PHP study notes object-oriented construction and destruction methods_PHP tutorial

WBOY
Release: 2016-07-21 15:28:46
Original
703 people have browsed it

Copy code The code is as follows:

/*
* 1. Access to members in the object ( In the internal method of an object, access other methods and member properties in the object)
* 2. There is a $this keyword by default in the methods in the object, which represents the method that calls this method. Object
*
* Constructor method
*
* 1. It is the "first" "automatically called" method after the object is created
*
* 2. Constructor method Definition, the method name is fixed,
* In php4: The method that is the same as the class name is the constructor method
* In php5: The constructor method is selected to use the magic method __construct() to declare the constructor in all classes All methods use this name
* Advantages: When changing the class name, the constructor method does not need to be changed
* 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), and there is no self-defined one
* Each magic method is a method that is automatically called at different times to complete a certain function
* Different Magic methods have different calling times
* All methods start with __
* __construct(); __destruct(); __set();...
*
* Function : Initialize member attributes;
*
*
* Destruction method
*
* 1. The last "automatically" called method before the object is released
* Use garbage Recycler (java php), and c++ manual release
*
* Function: close some resources and do some cleanup work
*
* __destruct();
*
*/
class Person{
var $name;
var $age;
var $sex;
//Construction method in php4
/*function Person()
{
//Every time an object is declared,
echo "1111111111111111";
}*/
//Constructor 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(){
}
//Destruction method
function __destruct(){
}
}
$p1=new Person("zhangsan",25,"male");
$p2=new Person;
$p3=new Person ;

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323520.htmlTechArticleCopy the code as follows: ?php /* * 1. Access to members in the object (in the internal method of an object , to access other methods and member properties in this object) * 2. In the methods in 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