php learning notes object oriented construction and destruction methods

  • 2020-05-07 19:25:42
  • OfStack

 
<?php 
/* 
* 1. Access to members of an object ( in 1 To access other methods and member properties in the local pair ) 
* 2. By default in all methods in the object 1 a $this Keyword, which represents the object calling the method  
* 
*  A constructor  
* 
* 1. Is the object created after the completion of the" 1 "Automatically call" methods  
* 
* 2. The definition of a constructor. The method name is 1 Fixed,  
*  in php4 In the : The method that is the same as the class name is the constructor  
*  in php5 In the : The constructor is chosen to use   Magic methods __construct()  All class declaration constructors use this name  
*  Advantage: the constructor does not have to change when changing the class name  
*  Magic methods:   When a magic method is written in a class, the corresponding function is added  
*  Method names are fixed ( It's all provided by the system ), It doesn't define itself  
*  every 1 A magic method, all at different times in order to accomplish something 1 Method called automatically by the function  
*  Different magic methods have different call times  
*  Are based on  __  Opening method  
* __construct(); __destruct(); __set();...... 
* 
*  Effect: initializes a member property;  
* 
* 
*  destructor  
* 
* 1. Last before the object is released 1 Methods called automatically  
*  Use a garbage collector (java php) And the c++ manual   The release of  
* 
*  Effect: close 1 Some resources, do 1 Some cleaning up  
* 
* __destruct(); 
* 
*/ 
class Person{ 
var $name; 
var $age; 
var $sex; 
//php4 Construction method in  
/*function Person() 
{ 
// Each statement 1 All objects are called  
echo "1111111111111111"; 
}*/ 
//php5 Construction method in  
function __construct($name,$age,$sex){ 
$this->name=$name; 
$this->age=$age; 
$this->sex=$sex; 
} 
function say(){ 
//$this->name;// Access to members of an object $this 
echo " My name: {$this->name} , my age: {$this->age}<br>" 
} 
function run(){ 
} 
function eat(){ 
} 
// destructor  
function __destruct(){ 
} 
} 
$p1=new Person("zhangsan",25," male "); 
$p2=new Person; 
$p3=new Person; 

Related articles: