魔术方法:

<?php
header('content-type:text/html;charset=utf-8');
/*
魔术方法:
__construct():
__destruct():
__set():
__get():
__toString():输出对象的时候会被自动调用,返回字符串
__invoke():当以调用函数的方式调用对象的时候,会自动触发这个方法
__call():当对象调用一个不可或者不存在访问的方法的时候,自动调用
__callStatic():用静态的方式调用一个不可访问的或者不存在的方法,会自动调用
__clone():克隆对象的时候自动调用
__wakeup():反序列化对象的时候会自动调用
__sleep():序列化对象的时候会自动调用

*/
class Test{
    public $username='king';
    protected $money=123456.89;
    private $age=12;
    public $test1='this is a test1';
    public function __toString(){
        echo '当输出对象的时候...我才自动调用<br/>';
        return 'hello object';
    }
    private function testPrivate(){
        echo 'this is private func<br/>';
    }
    public function __invoke(){
        print_r(func_get_args());
        echo '当以调用函数的方式调用对象的时候,会自动触发这个方法...<br/>';
    }
    public function __call($methodName,$args){
        echo 'methodName:'.$methodName.'<br/>';
        print_r($args);
    }
    private static function testPrivateStatic(){
        echo 'this is a private Static function...<br/>';
    }
    public static function __callStatic($methodName,$args){
        echo 'StaticMethodName:'.$methodName.'<br/>';
        print_r($args);
    }
    public function __sleep(){
        echo '对象被序列化了...<br/>';
        return array('username','age');
    }
    public function __wakeup(){
        echo '反序列化得时候自动调用...<br/>';
        $this->username='queen';
        echo $this->username.'<br/>';
    }
}
$test=new Test;
// echo $test;
$test(10,20,30,'test');
echo '<br/>';
$test->noExists(1,2,3,4,5);
echo '<hr/>';
$test->testPrivate();
echo '<hr/>';
Test::testPrivateStatic(123123);
echo '<hr/>';
Test::noExistsStatic();
echo '<hr/>';
$serializeStr=serialize($test);
unserialize($serializeStr);
原文地址:https://www.cnblogs.com/xujing6/p/6160578.html