Home > Article > Backend Development > photoshop learning website php learning notes object-oriented construction and destruction methods
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 attributes in the object) )
* 2. There is a $this keyword by default in the methods in the object. This keyword represents the object that calls this method
*
* Constructor method
*
* 1. After the object is created, "first "Automatically call" method
*
* 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: Constructor method selection Use magic method __construct() Use this name to declare constructors in all classes
* Advantages: When changing the class name, the constructor does not need to change
* Magic method: A certain magic method is written in the class, and this method corresponds to The function will be added
* The method names are all fixed (all provided by the system), 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 timings
* All methods begin with __
* __construct(); __destruct(); __set();...
*
* Function: Initialize member properties;
*
*
* Destruction method
*
* 1. The last "automatically" called method before the object is released
* uses 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()
{
//Every time an object is declared, it will be called
echo "1111111111111111";
}*/
//Construction method in php5
function __construct($name,$age,$sex){
$this->name=$name;
$this->age=$age;
$this->sex=$sex;
}
function say(){
//$this->name; //Use $this to access members in the object
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;
The above introduces the object-oriented construction and destruction methods of photoshop learning website and php learning notes, including the content of photoshop learning website. I hope it will be helpful to friends who are interested in PHP tutorials.