php--Magic Method

Source: Internet
Author: User

methods starting with twounderscores in PHP,__construct (), __destruct (), __call (), __callstatic (), __get (), __set (), __ Isset (), __unset (), __sleep (), __wakeup (), __tostring (), __set_state, () __clone () __autoload () , etc., are called " Magic Method "(Magicmethods).

Role:

Magic Method is the introduction of Object Oriented The further realization of the programming idea, the overloading is realizes by the Magic method.

To better support object-oriented

to achieve backward compatibility, if PHP 5 does not find the __construct () function in the class and does not inherit one from the parent class, it tries to find the old-fashioned constructor, which is the function with the same name as the class. So the only thing that can create a compatibility problem is when a method named __construct () already in the class is used for other purposes.

<?php
/**
* Created by Phpstorm.
* USER:FU
* DATE:2017/7/19
* time:14:38
*/

Class city{

protected $name;
Private $area;

function __construct ($name, $area) {
$this->name = $name;
$this->area = $area;
}

/**
* __get Magic method, which is automatically called when external access to the property is not directly accessible in the class
* Magic method has its default definition format, can not be arbitrarily changed
* Magic method cannot be added static modifier, except __callstatic
* __get Magic method must have a return value
* @param $name It corresponds to the name of the declared property in the class, formatted as a string
*/
function __get ($name) {
Echo ' I was called: '. $name;
if (Isset ($this-$name)) {
return $this $name;
}else{
return null;
}
}

/**
* __set Magic Method, automatically called when assigning a value to a property that cannot be directly accessed in a class
* @param $name The name of the property in the assigned class, string format
* @param $value assigned value
*/
function __set ($name, $value) {
Echo ' I was called: '. $name. ' = = = '. $value;
/*
* Property_exists detects whether a class or object contains a property of the specified name
*/
if (Property_exists ($this, $name)) {
$this $name = $value;
}else{
There is no corresponding attribute in the echo ' class: '. $name. ' <br> ';
}
}

function __isset ($name) {
if (Property_exists ($this, $name)) {
Return Isset ($this-$name);
}
}
function __unset ($name) {
Echo ' Properties to destroy: '. $name;
if (Property_exists ($this, $name)) {
$this, $name = null;
}else{
Unset ($this, $name);
}
}



protected function test () {
Echo ' <br> I can't directly access the test function method ';
}
protected function Test1 ($info) {
Echo ' <br> get Info: '. $info;
}
Private Function Test2 ($num 1, $num 2) {
echo ' <br> calculation result: '. ($num 1+ $num 2);
}

/**
* __call Magic Method
* @param $name The name of the function method in the class to be accessed, the format string
* @param $arguments parameter values for the corresponding function method, formatted as an array
*/
function __call ($name, $arguments) {
/*
* Method_exists A function method that determines whether a class contains a specified name
*/
if (Method_exists ($this, $name)) {
$this $name ();
Call_user_func (Array ($this, $name), $arguments [0]);
/*
* Call_user_func_array uses it to access the function method in the class, the number of arguments is arbitrary
*/
Call_user_func_array (Array ($this, $name), $arguments);
}else {
Echo ' did not find a corresponding function method <br> ';
}
}

/**
* __tostring Magic method is called automatically when a string is used to output the object of a class
* It requires that a string result must be returned
*/
function __tostring () {

Return ' <br> City name: '. $this->name ', area: '. $this->area;
}

/**
* __clone Magic Method, which is called automatically when the object of the class is cloned
* Default clone on shallow clone mode
* This method can be implemented to modify the value of some properties when cloning
*/
function __clone () {
Echo ' <br> called Clone's Magic Method <br> ';
$this->area = ' 3333 km² ';
}

}



$city = new City (' Chengdu ', ' 3000 km² ');
$city 2 = Clone $city;
Var_dump ($city);
Echo ' <br> ';
Var_dump ($city 2);
Echo ' <br> ';

Var_dump ($city);
Echo ' <br> ';
Print_r ($city);
Echo ' <br> ';
Print $city;
Echo $city;

$city->test ();
$city->test1 (' haha haha ');
$city->test2 (100,200);

Echo ' name: '. $city->name. ' <br> ';
Echo ' area: '. $city->area. ' <br> ';
Echo ' population: '. $city->people. ' <br> ';
Echo ' <br> ';
$city->name = ' Chongqing ';
$city->people = ' 15000000 ';
//
unset ($city->name);
//
Echo ' name: '. $city->name. ' <br> ';
Echo ' area: '. $city->area. ' <br> ';
Echo ' population: '. $city->people. ' <br> ';
Echo ' <br> ';
Var_dump ($city);

$test = false;
if (Isset ($test))
Echo ' OK ';

if (Isset ($city->people)) {
echo ' = = = Value = = = = = = =
}else{
echo ' = = = not get value = = ';
//}

php--Magic Method

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.