Home > Article > Backend Development > A condensed summary of the latest PHP classes in 2010_PHP Tutorial
1: Structure and call (instantiation):
class className{}, call: $obj = new className(); When the class has a constructor, parameters should also be passed in. Such as $obj = new className($v,$v2...);
2: Constructor and destructor :
1. The constructor is used for initialization: use __construct(), which can take parameters.
2. But the destructor cannot take parameters (used to perform some operations or functions before deleting a class). The destructor is named __destruct(). At the end of script execution, PHP will destroy the objects in the memory, so there is no need for a destructor function, but some, such as COOKIE, should be destroyed using this function.
Knowledge point: PHP4 also provides a constructor, but uses a class method with the same name as the class. This approach is still compatible with PHP5. When a class does not contain __construct, it will search for a method with the same name as the class. If found, it is considered to be a constructor, as follows:
class test
{ var $b;
function test() { $this->b=5; }
function addab($c) { return $this->b+$c; }
}
$a = new test(); echo $a->addab(4); // Return 9
3. PHP will not automatically call the constructor of the parent class (constructor overloading is not supported) and must be called explicitly using the parent keyword.
class employee{
Function __construct()....
}
class Manager extents Employee{
Function __construct(){
parent::_construct();
echo The parent class constructor of this subclass is called! ;
}
}
Of course, you can also call constructors of other classes that have nothing to do with the instance. Just add the class name before __construct(). Such as:
otherClassName::__construct();
Main family members of the class: properties, methods, constants, static members
3. Class attributes:
There are two ways to assign or get values to attributes of a class.
1. Use the public keyword in public scope.
2. Use __set() and __get() to assign and obtain values respectively. The former is called the setter method (setter) or the modification method (mutator), and the latter is called the accessor method (accessor) or getter method (getter). It is recommended to use this method: Advantages:
A. Data verification can be performed uniformly in __set().
B. Facilitate unified management of attributes.
Note:
First: __set() and __get() only work on private attributes. For attributes defined with public, they are both lazy, as follows:
class test{
protected $a=9,$b=2,$c;
Public $d;
Function __set($n,$v) { $this->$n = $v+2; }
Function __get($name) { return $this->$name+2; }
}
$a = new test();
$a->b =5; echo "
"; echo $a->b;
In the example, only the settings for $a, $b, and $c will be filtered and returned through __set and __get. For $d, it will not work. For example, $a->d=5, it will still return 5.
Second: __set($n,$v) takes two parameters. And __get($n) can only have one parameter. Example:
class test{
private $a=5,$b=6,$c;
Function __set($n,$v)
{
If($n==a&&$n>0)
$this->$n = $v;
else
}
Function __get($name)
{
return $this->$name; //If changed to return $this->$name + $this->addab(); If the value of a is called, the actual value returned is the value of a+a+b. The default is 5+5+6=16.
}
Function addab()
return $this->a + $this->b;
}
$e=new test();
$e->a = 11; //Note the writing: $this->$n is used inside the class, that is, the variable is written, but $e->a is used for external instances.
$e->b = 12; //get 14
$e->k = 22;
The attributes of a class can be freely extended, such as k in the above example. Regardless of whether __set is used or not, when an instance is established, you can use $e->newProperty = xx; to directly create an attribute, but this is not recommended. .