Overloading in PHP (i)

Source: Internet
Author: User

No matter what kind of programmer you are, or what language you use to program, I believe you must not be unfamiliar with overloading, but as a PHP overload, but unique, because it is the language of PHP (temporarily understood as the language, the script I do not like) decided its style, maybe you are a Java programmer, Then you must be very thorough about overloading, but PHP is not the same overload, then we compare the Java overload and PHP in the definition of overloading:

Overloaded definitions in 1.java and overloaded definitions in PHP (vernacular)

Java overloading: You can create multiple methods in a class that have the same name but have different parameters and different definitions. The method is called polymorphism by the number of different arguments passed to them and by the type of parameter to determine which method to use.

Overloading in PHP: reloading the processing of inaccessible members, called overloading of members (typically including property overloading and method overloading); Keep looking down ....

2, code comparison

Java:

Public function A ($a) {}

Public function A ($a, $b) {}//parameter differs from method with the same name

Php:

Public function A ($a) {}//this correct

Public function A ($a, $b) {}//incorrect definition

Property overloading in 3.php

By default, PHP is supported to reload the properties inside of the Object! This manifestation is called a property overload! PHP's access to inaccessible properties can be handled using the appropriate processing Method!

Use the Magic method provided by PHP to complete!

There are four ways to handle the 4 cases of attribute operation respectively!

Property refers to a property that is not accessible: a property that does not exist, and an access modifier that controls access to a

__set (), the property is assigned a value. When an assignment occurs, it is automatically executed to the __set () method, which is determined by the code within the set method to handle the property. Get two parameters, property names and property values!

__get () to read the property value. When a value is obtained for an inaccessible property, the method is called automatically, and the method obtains the current property name to manipulate it!

__unset, delete the attribute. This method is called automatically when the private attribute is deleted, and the property operation is removed according to the internal implementation unset ()

__isset () determines the current property value.

Class Student {
Private $stu _id = ' 2014 ';//needs to be read
Private $name = ' php ';//is read
Private $money = 0.0;//Read and set
Private $team _id = ' 2003 ';//Read and set

Operating interface
Public Function GetId () {}
Public Function GetName () {}
Public Function Getmoney () {}
Public Function Getteam () {}
//
Public Function Setmoney () {}
Public Function Setteam () {}



/**
* When assigning a value to an inaccessible property, it is automatically triggered
* @param $property _name string property name
* @param $property _value Mixed property value
*/
Public Function __set ($property _name, $property _value) {
$allow _write = Array (' Money ', ' team_id ');
if (In_array ($property _name, $allow _write)) {
The current property is readable
$this $property _name = $property _value;
}
}

Public Function __get ($property _name) {
$allow _read = Array (' stu_id ', ' name ', ' money ', ' team_id ');//All readable properties
if (In_array ($property _name, $allow _read)) {
The current property is readable
return $this $property _name;
}
}
}


$stu = new Student;
Echo $stu->stu_abcd;
Echo ' $stu->money = 3600.0;
Echo $stu->money;
Echo ' $stu->name = ' net ';
Echo $stu->name;

Echo ' $stu->new_p = ' hao123 ';
Echo $stu->new_p;
Var_dump ($stu);

__unset and __isset************************ ******************************************************

Class Student {
Private $stu _id = ' 2014 ';//needs to be read
Private $name = ' php ';//is read
Private $money = 0.0;//Read and set
Private $team _id = ' 2003 ';//Read and set


Public Function __unset ($p _name) {
This property can be deleted, and the Unset method is executed internally
if ($p _name = = ' team_id ') {
Unset ($this, $p _name);
}
}

Public Function __isset ($p _name) {
if ($p _name = = ' name ' | | $p _name = = ' team_id ') {
return true;
} else {
return false;
}

}
}


$o = new Student;
Var_dump (Isset ($o->stu_id));
Var_dump (Isset ($o->name));

Exit
unset ($o->stu_id);
unset ($o->team_id);
Var_dump ($o);

Summary: The above basic is the overload of the basic use of property overloading, then the next section, there are methods overloaded ...


Overloading in PHP (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.