php中的魔術方法

來源:互聯網
上載者:User

標籤:

php 中的魔術方法:

<?phpclass cat{    public function  __get($property)    {        return  $this->{$property};    }        public function __call($parm1,$parm2)    {        print_r($parm1);        print_r($parm2);    }    public function __set($var,$val)    {            echo $var."|".$val."\r\n";            $this->$var = $val;    }    public function __clone()    {        echo "clone func invoke"."\r\n";        }    public function __isset($var)    {        echo "via __isset view isset".$var;    }    public function __unset($var)    {        echo "via __unset unset".$var;    }    public static function __callStatic($parm1,$parm2)    {        echo "__callStatic ".$parm1.var_dump($parm2);    }    public function __construct()    {        echo ‘__construct‘;    }    public function __destruct()    {        echo ‘__destruct‘;    }    public function __toString()    {        return  ‘cat to string‘;    }    public function __invoke($xxx)    {            var_dump($xxx);    }    }$Cat = new cat();//echo $Cat->name;//$Cat->hello($array=array(‘a‘,‘b‘));$Cat->a = 3;$Cat2 = clone $Cat;$Cat2->a = 10;echo $Cat->a;echo $Cat2->a;var_dump(isset($Cat->b));unset($Cat->b);Cat::ddd(‘abc‘);echo $Cat2;$Cat(‘abccd‘);var_dump(is_callable($Cat));

此外還有:

<?php//student.class.phpclass Student{    private $full_name = ‘‘;    private $score = 0;    private $grades = array();        public function __construct($full_name, $score, $grades)    {        $this->full_name = $full_name;        $this->grades = $grades;        $this->score = $score;    }        public function show()    {        echo $this->full_name;    }        function __sleep()    {        echo ‘Going to sleep...‘;        return array(‘full_name‘, ‘grades‘, ‘score‘);    }        function __wakeup()    {        echo ‘Waking up...‘;    }}?><?php//a.php$student = new Student(‘bla bla‘, ‘a‘, array(‘a‘ => 90, ‘b‘ => 100));$student->show();echo "<br />\n";$s = serialize($student);echo $s ."<br />\n";$fp = fopen(‘./session.s‘, ‘w‘);fwrite($fp, $s);fclose($fp);?><?php//b.php$s = implode(‘‘, file("./session.s"));echo $s ."<br />\n";$a = unserialize($s);$a->show();?>

其中__invoke是5.3以後加入的。

php中的魔術方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.