<?php
class A
{
public $publicName = " public name";
private $privateName = " private name";
protected $protectedName = " protected name";
//类初始化的时候执行
public function __construct()
{
var_dump("__construct:" . time());
}
//调用类的属性不存在 或者 非公有属性的时候会执行
public function __get($name)
{
var_dump("__get:" . time() . " name:" . $name);
}
//设置类的属性不存在 或者 非公有属性的时候会执行
public function __set($name, $value)
{
var_dump("__set:" . time() . " name:" . $name . ',value:' . $value);
}
//判断类的属性是否存在 若name对应的属性不存在 或者非公有属性时会执行
public function __isset($name)
{
var_dump("__isset:" . time() . " name:" . $name);
}
//删除类的属性是否存在 若name对应的属性不存在 或者非公有属性时会执行
public function __unset($name)
{
var_dump("__unset:" . time() . " name:" . $name);
}
//序列化对象的时候触发
public function __sleep()
{
var_dump("__sleep:" . time());
return ['publicName'];
}
//反序列化对象的时候触发
public function __wakeup()
{
var_dump("__wakeup:" . time());
}
//克隆函数的时候触发 可以在这里防止克隆
public function __clone()
{
var_dump("__clone:" . time());
}
//对象转字符串输出
public function __toString()
{
var_dump("__toString:" . time());
return "";//必须返回string
}
//调试信息 这里对打印的 类信息进行加工
public function __debugInfo()
{
return ['publicName' => $this->publicName . ' hang'];
}
//把类当做函数一样使用时调用时触发
public function __invoke()
{
var_dump("__invoke:" . time());
}
//调用类不存在的方法时候执行
public function __call($name, $arguments)
{
//此类必须要2个参数 第一个参数是函数名 第二个参数是请求值对应的是调用函数时传递进来的参数
var_dump("__call:" . time() . ' name:' . $name . ',arguments:' . json_encode($arguments));
}
//调用类不存在的静态方法时候执行
static public function __callStatic($name, $arguments)
{
var_dump("__callStatic:" . time() . ' name:' . $name . ',arguments:' . json_encode($arguments));
}
//类执行完的时候执行
public function __destruct()
{
var_dump("__destruct:" . time());
}
}
$obj = new A();
/**
* string(22) "__construct:1572225812"
* string(21) "__destruct:1572225812"
*/
$obj->noFunction();
/**
* string(46) "__call:1572225930 name:noFunction,arguments:[]"
*/
$obj::noStaticFunction(1);
/**
* string(59) "__callStatic:1572226262 name:noStaticFunction,arguments:[1]"
*/
$obj->publicName;
$obj->privateName;
$obj->protectedName;
$obj->publicA;
/**
* string(33) "__get:1572226958 name:privateName"
* string(35) "__get:1572226958 name:protectedName"
* string(35) "__get:1572226958 name:publicA"
*/
$obj->publicName = "long";
//var_dump("console log :set value -> publicName = ".$obj->publicName);
$obj->privateName = "long";
$obj->protectedName = "long";
$obj->publicA = "long";
/**
* string(44) "__set:1572227494 name:privateName,value:long"
* string(46) "__set:1572227494 name:protectedName,value:long"
* string(40) "__set:1572227494 name:publicA,value:long"
*/
isset($obj->publicName);//bool(true)
isset($obj->privateName);
isset($obj->protectedName);
isset($obj->publicA);
empty($obj->publicB);//empty也会调用 __isset()
/**
* string(35) "__isset:1572227785 name:privateName"
* string(37) "__isset:1572227785 name:protectedName"
* string(31) "__isset:1572227785 name:publicA"
*/
//unset($obj->publicName);
//var_dump($obj->publicName);//删除类的属性之后再调用会触发 __get()
unset($obj->privateName);
unset($obj->protectedName);
unset($obj->publicB);
/**
* string(35) "__unset:1572228345 name:privateName"
* string(37) "__unset:1572228345 name:protectedName"
* string(31) "__unset:1572228345 name:publicB"
*/
$serialize_value = serialize($obj);
//O:1:"A":1:{s:10:"publicName";s:4:"long";}
/**
* string(18) "__sleep:1572229299"
*/
$obj2 = unserialize($serialize_value);
/**
* string(19) "__wakeup:1572230195"
* string(21) "__destruct:1572230958" //这里会多调用一次 析构函数 因为对象obj2
*/
$obj3 = clone $obj;
/**
* string(18) "__clone:1572231071"
* string(21) "__destruct:1572231071" //这里会再多调用一次析构函数
*/
echo $obj;
/**
* string(21) "__toString:1572231328"
*/
//var_dump($obj);
/**
* 未定义 debugInfo时
* object(A)#1 (3) {
* ["publicName"]=>string(4) "long"
* ["privateName":"A":private]=>string(13) " private name"
* ["protectedName":protected]=>string(15) " protected name"
* }
* 定义 debugInfo后
* object(A)#1 (1) {
* ["publicName"]=>string(9) "long hang"
* }
*/
$obj(1);
/**
* string(19) "__invoke:1572232068"
*/
上一篇: swoole 学习笔记...
下一篇: Mysql中查询笔记...
糖糖(金牌售后) 2019-11-30 11:10 Win 7 广西桂林