首页  >  文章  >  后端开发  >  php中重载与魔术方法的示例详解

php中重载与魔术方法的示例详解

黄舟
黄舟原创
2017-11-02 09:16:101218浏览

定义:PHP 将所有以 (两个下划线)开头的类方法保留为魔术方法。所以在定义类方法时,除了上述魔术方法,建议不要以  为前缀。

作用:利用模式方法可以轻松实现PHP面向对象中重载(Overloading即动态创建类属性和方法

其实很多博客主已经写过这些方法了、、、但是我还是要说一遍。。谁让你来听了呢,码这么多字也不容易看完再走吧!

1.construct,destruct
 constuct构建对象的时被调用;
 destruct明确销毁对象或脚本结束时被调用;  

class Foo {    
private $name;    
private $link;    
public function construct($name) {        
$this->name = $name;
    }    
    public function destruct() {        
    echo 'Destroying: ', $this->name, PHP_EOL;//PHP_EOL代表php的换行符    }
}

打住,先安利一波重载的定义及使用方法:

定义:PHP所提供的"重载"(overloading)是指动态地"创建"类属性和方法。(我们是通过魔术方法(magic methods)来实现的)

作用:当调用当前环境下未定义或不可见的类属性或方法时,重载方法会被调用。后面将使用"不可访问属性(inaccessible properties)"和"不可访问方法(inaccessible methods)"来称呼这些未定义或不可见的类属性或方法。

注意:所有的重载方法都必须被声明为 public

 2.get、set、isset、unset、call、callStatic  //为啥放一起呢?


 set当给不可访问或不存在属性赋值时被调用
 get读取不可访问或不存在属性时被调用

isset对不可访问或不存在的属性调用isset()或empty()时被调用
 unset对不可访问或不存在的属性进行unset时被调用

call调用不可访问或不存在的方法时被调用
 callStatic调用不可访问或不存在的静态方法时被调用

Example #1 使用 get(),set(),isset() 和 unset() 进行属性重载

<?php
class PropertyTest {
     /**  被重载的数据保存在此  */
    private $data = array();

 
     /**  重载不能被用在已经定义的属性  */
    public $declared = 1;

     /**  只有从类外部访问这个属性时,重载才会发生 */
    private $hidden = 2;

    public function set($name, $value) 
    {
        echo "Setting &#39;$name&#39; to &#39;$value&#39;\n";
        $this->data[$name] = $value;
    }


    public function get($name) 
    {
        echo "Getting &#39;$name&#39;\n";
        if (array_key_exists($name, $this->data)) {
            return $this->data[$name];
        }

        $trace = debug_backtrace();
        trigger_error(
            &#39;Undefined property via get(): &#39; . $name .
            &#39; in &#39; . $trace[0][&#39;file&#39;] .
            &#39; on line &#39; . $trace[0][&#39;line&#39;],
            E_USER_NOTICE);
        return null;
    }



    /**  PHP 5.1.0之后版本 */
    public function isset($name) 
    {
        echo "Is &#39;$name&#39; set?\n";
        return isset($this->data[$name]);
    }

    /**  PHP 5.1.0之后版本 */
    public function unset($name) 
    {
        echo "Unsetting &#39;$name&#39;\n";
        unset($this->data[$name]);
    }

    /**  非魔术方法  */
    public function getHidden() 
    {
        return $this->hidden;
    }
}


echo "<pre class="brush:php;toolbar:false">\n";

$obj = new PropertyTest;

$obj->a = 1;
echo $obj->a . "\n\n";

var_dump(isset($obj->a));
unset($obj->a);
var_dump(isset($obj->a));
echo "\n";

echo $obj->declared . "\n\n";

echo "Let&#39;s experiment with the private property named &#39;hidden&#39;:\n";
echo "Privates are visible inside the class, so get() not used...\n";
echo $obj->getHidden() . "\n";
echo "Privates not visible outside of class, so get() is used...\n";
echo $obj->hidden . "\n";
?>

看完了吗?好好看!

5.sleep,wakeup
sleep当使用serialize时被调用,当你不需要保存大对象的所有数据时很有用

wakeup当使用unserialize时被调用,可用于做些对象的初始化操作

<?php
class Connection 
{
    protected $link;
    private $server, $username, $password, $db;
    
    public function construct($server, $username, $password, $db)
    {
        $this->server = $server;
        $this->username = $username;
        $this->password = $password;
        $this->db = $db;
        $this->connect();
    }
    
    private function connect()
    {
        $this->link = mysql_connect($this->server, $this->username, $this->password);
        mysql_select_db($this->db, $this->link);
    }
    
    public function sleep()
    {
        return array(&#39;server&#39;, &#39;username&#39;, &#39;password&#39;, &#39;db&#39;);
    }
    
    public function wakeup()
    {
        $this->connect();
    }
}
?>

6.clone

 进行对象clone时被调用,用来调整对象的克隆行为

<?php
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()
    {
      
        // 强制复制一份this->object, 否则仍然指向同一个对象
        $this->object1 = clone $this->object1;
    }
}

$obj = new MyCloneable();

$obj->object1 = new SubObject();
$obj->object2 = new SubObject();

$obj2 = clone $obj;


print("Original Object:\n");
print_r($obj);

print("Cloned Object:\n");
print_r($obj2);

?>

7.toString
 当一个类被转换成字符串时被调用

<?php
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()
    {
      
        // 强制复制一份this->object, 否则仍然指向同一个对象
        $this->object1 = clone $this->object1;
    }
}

$obj = new MyCloneable();

$obj->object1 = new SubObject();
$obj->object2 = new SubObject();

$obj2 = clone $obj;


print("Original Object:\n");
print_r($obj);

print("Cloned Object:\n");
print_r($obj2);

?>

8.set_state
 当调用var_export()导出类时,此静态方法被调用。用set_state的返回值做为var_export的返回值。

<?php

class A
{
    public $var1;
    public $var2;

    public static function set_state($an_array) // As of PHP 5.1.0
    {
        $obj = new A;
        $obj->var1 = $an_array[&#39;var1&#39;];
        $obj->var2 = $an_array[&#39;var2&#39;];
        return $obj;
    }
}

$a = new A;
$a->var1 = 5;
$a->var2 = &#39;foo&#39;;

eval(&#39;$b = &#39; . var_export($a, true) . &#39;;&#39;); // $b = A::set_state(array(
                          //    &#39;var1&#39; => 5,
                          //    &#39;var2&#39; => &#39;foo&#39;,
                          // ));
var_dump($b);
?>

以上是php中重载与魔术方法的示例详解的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn