欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php教程之魔术方法的使用示例(php魔术函数)

程序员文章站 2024-01-27 19:03:05
复制代码 代码如下:/** php把所有以__(两个下划线)开头的类方法当成魔术方法。所以你定义自己的类方法时,不要以 __为前缀。 * */ // __tostring...

复制代码 代码如下:

/** php把所有以__(两个下划线)开头的类方法当成魔术方法。所以你定义自己的类方法时,不要以 __为前缀。 * */

// __tostring、__set、__get__isset()、__unset()
/*
  the __tostring method allows a class to decide how it will react when it is converted to a string.
  __set() is run when writing data to inaccessible members.
  __get() is utilized for reading data from inaccessible members.
  __isset() is triggered by calling isset() or empty() on inaccessible members.
  __unset() is invoked when unset() is used on inaccessible members.
 */
class testclass {

    private $data = array();
    public $foo;

    public function __construct($foo) {
        $this->foo = $foo;
    }

    public function __tostring() {
        return $this->foo;
    }

    public function __set($name, $value) {
        echo "__set, setting '$name' to '$value'\n";
        $this->data[$name] = $value;
    }

    public function __get($name) {
        echo "__get, getting '$name'\n";
        if (array_key_exists($name, $this->data)) {
            return $this->data[$name];
        }
    }

    /** as of php 5.1.0 */
    public function __isset($name) {
        echo "__isset, is '$name' set?\n";
        return isset($this->data[$name]);
    }

    /** as of php 5.1.0 */
    public function __unset($name) {
        echo "__unset, unsetting '$name'\n";
        unset($this->data[$name]);
    }

}

$obj = new testclass('hello');
echo "__tostring, $obj\n";
$obj->a = 1;
echo $obj->a . "\n\n";
var_dump(isset($obj->a));
unset($obj->a);
var_dump(isset($obj->a));
echo "\n\n";
/**
  输出结果如下:
  __tostring, hello
  __set, setting 'a' to '1'
  __get, getting 'a'
  __isset, is 'a' set?
  bool(true)
  __unset, unsetting 'a'
  __isset, is 'a' set?
  bool(false)
 **/

 

// __call  __callstatic
/*
  mixed __call ( string $name , array $arguments )
  mixed __callstatic ( string $name , array $arguments )
  __call() is triggered when invoking inaccessible methods in an object context.
  __callstatic() is triggered when invoking inaccessible methods in a static context.
  the $name argument is the name of the method being called.
  the $arguments argument is an enumerated array containing the parameters passed to the $name'ed method.
 */
class methodtest {
    public function __call($name, $arguments) {
        // note: value of $name is case sensitive.
        echo "__call, calling object method '$name' " . implode(', ', $arguments) . "\n";
    }

    /** as of php 5.3.0 */
    public static function __callstatic($name, $arguments) {
        // note: value of $name is case sensitive.
        echo "__callstatic, calling static method '$name' " . implode(', ', $arguments) . "\n";
    }

}

$obj = new methodtest;
$obj->runtest('in object context', 'param2', 'param3');
//methodtest::runtest('in static context'); // as of php 5.3.0
echo "\n\n";
/**
 输出结果如下:
 __call, calling object method 'runtest' in object context, param2, param3
  string(10) "__invoke: "
 */

 

// __invoke
/*
  the __invoke method is called when a script tries to call an object as a function.
  note: this feature is available since php 5.3.0.
*/
class callableclass {
    function __invoke($x) {
        var_dump($x);
    }
}

$obj = new callableclass;
//$obj(5);
var_dump('__invoke: ' . is_callable($obj));
echo "\n\n";

 

 

// __sleep  __wakeup
/*
  串行化serialize可以把变量包括对象,转化成连续bytes数据. 你可以将串行化后的变量存在一个文件里或在网络上传输.
  然后再反串行化还原为原来的数据. 你在反串行化类的对象之前定义的类,php可以成功地存储其对象的属性和方法.
  有时你可能需要一个对象在反串行化后立即执行. 为了这样的目的,php会自动寻找__sleep和__wakeup方法.
  当一个对象被串行化,php会调用__sleep方法(如果存在的话). 在反串行化一个对象后,php 会调用__wakeup方法.
  这两个方法都不接受参数. __sleep方法必须返回一个数组,包含需要串行化的属性. php会抛弃其它属性的值.
  如果没有__sleep方法,php将保存所有属性.下面的例子显示了如何用__sleep和__wakeup方法来串行化一个对象.
  id属性是一个不打算保留在对象中的临时属性. __sleep方法保证在串行化的对象中不包含id属性.
  当反串行化一个user对象,__wakeup方法建立id属性的新值. 这个例子被设计成自我保持.
  在实际开发中,你可能发现包含资源(如图像或数据流)的对象需要这些方法
 */

class user {

    public $name;
    public $id;

    function __construct() {
        //give user a unique id 赋予一个差别 的id
        $this->id = uniqid();
    }

    //__sleep返回值的类型是数组,数组中的值是不需要串型化的字段id

    function __sleep() {
        //do not serialize this->id 不串行化id
        return(array("name"));
    }

    function __wakeup() {
        //give user a unique id
        $this->id = uniqid();
    }

}

//create object 成立一个器材
$u = new user;
$u->name = "leon"; //serialize it 串行化 留意不串行化id属性,id的值被遗弃
$s = serialize($u);
echo "__sleep, __wakeup, s: $s"; //unserialize it 反串行化 id被重新赋值
$u2 = unserialize($s); //$u and $u2 have different ids $u和$u2有差别 的id
print_r($u);
print_r($u2);
echo "\n\n";
/**
 输出结果如下:
  __sleep, __wakeup, s: o:4:"user":1:{s:4:"name";s:4:"leon";}
  user object
  (
  [name] => leon
  [id] => 4db1b17640da1
  )
  user object
  (
  [name] => leon
  [id] => 4db1b17640dbc
  )
 */


// __set_state
/*
  this static method is called for classes exported by var_export() since php 5.1.0.
  the only parameter of this method is an array containing exported properties in the form array('property' => value, ...).
 */

class a {

    public $var1;
    public $var2;

    public static function __set_state($an_array) { // as of php 5.1.0
        //$an_array打印出来是数组,而不是调用时传递的对象
        print_r($an_array);
        $obj = new a;
        $obj->var1 = $an_array['var1'];
        $obj->var2 = $an_array['var2'];
        return $obj;
    }

}

$a = new a;
$a->var1 = 5;
$a->var2 = 'foo';
echo "__set_state:\n";
eval('$b = ' . var_export($a, true) . ';');
// $b = a::__set_state(array(
// 'var1' => 5,
// 'var2' => 'foo',
// ));
var_dump($b);
echo "\n\n";
/**
  输出结果如下:
  __set_state:
  array
  (
  [var1] => 5
  [var2] => foo
  )
  object(a)#5 (2) {
  ["var1"]=>
  int(5)
  ["var2"]=>
  string(3) "foo"
  }
 */

 

// __clone
class subobject {

    static $instances = 0;
    public $instance;

    public function __construct() {
        $this->instance = ++self::$instances;
    }

    public function __clone() {
        $this->instance = ++self::$instances;
    }

}

class mycloneable {

    public $object1;
    public $object2;

    function __clone() {
        // force a copy of this->object, otherwise
        // it will point to same object.
        $this->object1 = clone $this->object1;
    }

}

$obj = new mycloneable();
$obj->object1 = new subobject();
$obj->object2 = new subobject();
$obj2 = clone $obj;
print("__clone, original object:\n");
print_r($obj);
print("__clone, cloned object:\n");
print_r($obj2);
echo "\n\n";
/**
 输出结果如下:
 __clone, original object:
  mycloneable object
  (
  [object1] => subobject object
  (
  [instance] => 1
  ) [object2] => subobject object
  (
  [instance] => 2
  ))
  __clone, cloned object:
  mycloneable object
  (
  [object1] => subobject object
  (
  [instance] => 3
  ) [object2] => subobject object
  (
  [instance] => 2
  ))
 */