<?php
/**
* Created by Phpstorm.
* USER:FU
* DATE:2017/7/19
* time:9:41
*/
Class person{
protected $name;
Private $age;
Private $mobile;
function __construct ($name, $age)
{
$this->name = $name;
$this->age = $age;
}
/**
* Tests using a function that is not directly accessible
* @param $info
*/
protected function Test1 ($info) {
Echo ' My message: '. $info;
}
Private Function Test2 () {
Echo ' I can't be directly visited Oh ~! ! ~ ';
}
// /**
* @return Mixed
// */
Public Function GetName ()
// {
return $this->name;
// }
/**
* __get Magic Method, the function method is called automatically when a property that is not directly accessible is called outside of the class
* The following format is the standard form of the Magic method and cannot be changed
* The Magic method must be public and cannot be defined as static, except __callstatic
* __get must have a return value, so the final end is definitely a return statement
* @param $name The name of the property being accessed, it must be a string
*/
Public Function __get ($name) {
Echo ' finds the property of the corresponding name in the class: '. $name;
if (Isset ($this-$name)) {
return $this $name;
}else{
return null;
}
}
/**
* Magic Method __set, when you set a property that is not directly accessible in a class, the function method is called automatically
* The following format is the standard form of the Magic method and cannot be changed
* __set does not need return value
* @param $name The property name of the class that needs to set the value
* @param $value The value you want to set
*/
Public Function __set ($name, $value) {
/*
* Property_exists It is used to determine whether a class contains a property of a specified name
*/
if (Property_exists ($this, $name)) {
$this $name = $value;
}
}
/**
* Magic method to determine if a property in a class is set
* __isset () is called when isset () or empty () is called on a non-accessible property.
* @param $name passed the property name of the class
* @return BOOL Returns true if the attribute exists, or false if it does not exist
*/
function __isset ($name) {
if (Property_exists ($this, $name)) {
Return Isset ($this-$name);
}else{
return false;
}
}
function __unset ($name) {
if (Property_exists ($this, $name)) {
$this, $name = null;
}else{
return null;
}
}
/**
* __call () is called automatically when a non-static method is called in an object that cannot be accessed directly.
* @param $name The name of the function method being accessed, string format
* @param the parameters of the function method passed by $arguments, array format
*/
function __call ($name, $arguments) {
Echo ' Name of the function to be accessed: '. $name;
Var_dump ($arguments);
if (Method_exists ($this, $name)) {
Call_user_func_array (Array ($this, $name), $arguments);
}else{
echo ' no corresponding function can be called ';
}
}
/**
* Magic method, when an object of class is output in string format, is automatically called by default
* The function method end must have a return statement, and the value returned must be a string
*/
function __tostring () {
Return ' name: '. $this->name. ', Age: '. $this->age;
return 100;
}
/**
* Automatic call execution when object of class is cloned
* The cloned data values can be modified here
*/
function __clone () {
Echo ' <br> data object was copied with a backup <br> ';
$this->age = 35;
}
}
$p = new Person (' John Doe ', 30);
$p->test1 (' Chengdu ');
Echo ' <br><br> ';
$p->test2 ();
Echo ' <br><br> ';
$p->test3 ();
Echo ' <br><br> ';
Echo $p;
Print $p;
Echo ' <br><br> ';
Print_r ($p);
Echo ' <br><br> ';
$p 2 = Clone $p;
$p 2->name = ' Harry ';
Print_r ($p 2);
Echo ' <br><br> ';
Var_dump ($p);
Echo $p->name;
Echo $p->age;
Echo $p->mobile;
Var_dump ($p);
//
//
$p->name = ' Chengdu ';
$p->mobile = ' 13066666666 ';
$p->birth = ' 2000-01-01 ';
//
unset ($p->age);
Echo ' <br><br> modified object value: ';
Var_dump ($p);
//
//
if (Isset ($p->name)) {
Echo ' OK ';
}else{
Echo ' No ';
//}
PHP Object-Oriented Magic method