面向对象的模式方法
面向对象的模式方法在使用中还是听常见的
/**
* 面向对象的魔术方法
*/
class ClassName
{
public $sex;//设置类的属性
/**
* 构造方法
*/
function __construct()
{
# code...
}
/**
* 当对象被当成字符串别调用的时候
* @return string [description]
*/
function __toString(){
return "this is tostring
";
}
/**
* 当对象被当作方法调用的时候
* @param [type] $ke [description]
* @return [type] [description]
*/
function __invoke($ke){
echo "this is invoke" . $ke;
}
/**
* 当方法未定义的时候调用
* @param [type] $name [description]
* @param [type] $param [description]
* @return [type] [description]
*/
function __call($name,$param){
echo "
";
echo $name . ":" . implode(',', $param);
}
/**
* 当静态调用方法不存在的时候出发
* @param [type] $name [description]
* @param [type] $param [description]
* @return [type] [description]
*/
static function __callStatic($name,$param){
echo "
";
echo $name . ":" . implode(',', $param);
}
/**
* 当调用的属性未定义的时候
* @param [type] $name [description]
* @return [type] [description]
*/
function __get($name){
return "
this is __get:" . $name;
}
/**
* 当属性被赋值的时候
* @param [type] $name [description]
* @param [type] $value [description]
*/
function __set($name,$value){
echo "
set the " . $name . " value is " . $value;
}
/**
* 当调用是否存在或者其他判断的时候
* @param [type] $name [description]
* @return boolean [description]
*/
function __isset($name){
echo "
this is __isset";
}
/**
* 当unset的时候
* @param [type] $name [description]
*/
function __unset($name){
echo "
this is __unset";
}
/**
* 当clone一个对象的时候
* @return [type] [description]
*/
function __clone(){
$this->sex = "clone";
}
}
$obj = new ClassName();
echo $obj;
$obj(5);
$obj->run(12,12);
$obj::run(23,54);
echo $obj->sex;
$obj->sex = "男";
echo $obj->sex;
//isset($obj->sex);
/*empty($obj->sex);
unset($obj->sex);*/
$obj2 = $obj;
echo $obj2->sex;
$obj2->sex = "女";
echo $obj->sex;
echo $obj2->sex;
$obj3 = clone $obj;
echo $obj3->sex;
$obj3->sex = "不知道";
echo $obj->sex;
echo $obj2->sex;
echo $obj3->sex;
请先登录后再评论登录
单行、唯一
1楼
回复