A few notes on the improved object-oriented approach to PHP Page 1/2_PHP Tutorial

WBOY
Release: 2016-07-21 15:48:59
Original
783 people have browsed it

先看代码: 

复制代码 代码如下:

class StrictCoordinateClass {
private $arr = array('x' => NULL, 'y' => NULL);
function __construct()
{
print "StrictCoordinateClass is being created";
print "
";
}
function __destruct()
{
print "
";
print "StrictCoordinateClass is being destroyed";
}
function __get($property)
{
if (array_key_exists($property, $this->arr)) {
return $this->arr[$property];
} else {
print "Error: Can't read a property other than x & yn";
}
}
function __set($property, $value)
{
if (array_key_exists($property, $this->arr)) {
$this->arr[$property] = $value;
} else {
print "Error: Can't write a property other than x & yn";
}
}
}
$obj = new StrictCoordinateClass();
$obj->x = 1;
print $obj->x;
print "
";
$obj->n = 2;
print "
";
print $obj->n;
?>

Output result:
StrictCoordinateClass is being created
1
Error: Can't write a property other than x & y
Error: Can't read a property other than x & y
StrictCoordinateClass is being destroyed
__construct() and __destruct() are equivalent to the constructor in Java and the destructor in C.
As for __get and __set, please see below:
Reference from: http://www.phpchina.com/html/54/26354-31906.html
.__set() __get() __isset( ) Application of the four methods of __unset()
Generally speaking, always define the attributes of a class as private, which is more in line with realistic logic. However, reading and assigning operations to attributes are very frequent, so in PHP5, two functions "__get()" and "__set()" are predefined to obtain and assign their attributes, as well as "__isset" to check the attributes. ()" and the method to delete attributes "__unset()".
In the previous section, we set and obtained methods for each attribute. PHP5 provides us with special methods for setting and obtaining values ​​for attributes, "__set()" and "__get()" These two methods, these two methods do not exist by default, but we add them to the class manually. Like the constructor method (__construct()), it will only exist if it is added to the class. You can add it in the following way Of course, these two methods can also be added according to personal style:
//__get() method is used to obtain private properties
private function__get($property_name)
{
if(isset($ this->$property_name))
{
return($this->$property_name);
}else
{
return(NULL);
}
}
//__set() method is used to set private properties
private function__set($property_name,$value)
{
$this->$property_name=$value;
}
__get() method: This method is used to get the private member attribute value. It has one parameter. The parameter is passed in the name of the member attribute you want to get, and the obtained attribute value is returned. This method does not need to be called manually, because We can also make this method a private method, which is automatically called by the object when the private property is directly obtained. Because the private properties have been encapsulated, the value cannot be obtained directly (for example: "echo $p1->name" is wrong to obtain directly), but if you add this method to the class, use " When a statement like "echo $p1->name" directly obtains the value, the __get($property_name) method will be automatically called, and the property name will be passed to the parameter $property_name. Through the internal execution of this method, the private value we passed in will be returned. The value of the attribute. If the member properties are not encapsulated as private, the object itself will not automatically call this method.
__set() method: This method is used to set values ​​for private member attributes. It has two parameters. The first parameter is the name of the attribute you want to set the value for, and the second parameter is the value you want to set for the attribute. , no return value. This method also does not need to be called manually. It can also be made private. It is automatically called when directly setting the private attribute value. The same private attribute has been encapsulated with
. If there is no __set() Methods are not allowed, for example: $this->name='zhangsan', this will cause an error, but if you add the __set($property_name, $value) method to the class, you can directly set the private property When assigning a value, it will be automatically called, passing the attribute such as name to $property_name, and passing the value "zhangsan" to be assigned to $value. Through the execution of this method, the purpose of assignment is achieved. If the member properties are not encapsulated as private, the object itself will not automatically call this method. In order not to pass in illegal values, you can also make a judgment in this method. The code is as follows:
classPerson
{
//The following are the member attributes of the person, which are all encapsulated private members
private $name; //The person’s name
private $sex; //Person’s gender
private $age; //Person’s age
//__get() method is used to obtain private properties
private function__get($property_name)
{
echo"When directly obtaining the private property value, this __get() method is automatically called
";
if(isset($this->$property_name))
{
return($this->$property_name);
}
else
{
return(NULL);
}
}
//__set() method Used to set private properties
private function__set($property_name,$value)
{
echo" When directly setting the value of a private property, this __set() method is automatically called to assign a value to the private property< br>";
$this->$property_name=$value;
}
}
$p1=newPerson();
//The operation of directly assigning a value to a private property will Automatically call the __set() method to assign values ​​
$p1->name="Zhang San";
$p1->sex="Male";
$p1->age=20;
//Get the value of the private attribute directly, the __get() method will be automatically called to return the value of the member attribute
echo "Name:".$p1->name."
";
echo"Gender:".$p1->sex."
";
echo"Age:".$p1->age."
";
?> ;

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/319639.htmlTechArticleLook at the code first: Copy the code as follows: ?php class StrictCoordinateClass { private $arr = array('x' = NULL, 'y' = NULL); function __construct() { print "StrictCoordinateClass is...
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!