故此在定义类方法时betvictor1946,除了上述魔术点子

第3你要明了怎么是php的魔术点子,它不是变魔术的,借使你想学习变魔术来错地方了哦!

首先你要掌握怎么着是php的魔术点子,它不是变魔术的,借使你想深造变魔术来错地点了哦!

定义:PHP 将有所以
__(五个下划线)初叶的类措施保存为魔术点子。所以在定义类方法时,除了上述魔术点子,建议不要以
__ 为前缀。

定义:PHP 将享有以
__(多少个下划线)开端的类情势保存为魔术点子。所以在定义类方法时,除了上述魔术点子,提出不用以
__ 为前缀。

作用:利用方式方法能够轻松完成PHP面向对象中重载(Overloading即动态创立类属性和办法)

作用:利用情势方法能够轻松达成PHP面向对象中重载(Overloading即动态创立类属性和方式)

实则过多博客主已经写过这一个点子了、、、可是自身要么要说贰回。。什么人让您来听了吗,码这么多字也不易于看完再走呢!

实则过多博客主已经写过这么些主意了、、、不过本身恐怕要说一遍。。谁让您来听了啊,码这么多字也不便于看完再走吗!

1.__construct,__destruct
__constuct营造对象的时被调用;
__destruct显明销毁对象或脚本甘休时被调用;

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的换行符
    }
}
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)来兑现的)

定义:PHP所提供的”重载“(overloading)是指动态地”创制”类属性和方法。(大家是通过魔术点子(magic
methods)来落到实处的)

作用:当调用当前环境下未定义或不可见的类属性或格局时,重载方法会被调用。后边将接纳”不可访问属性(inaccessible properties)”和”不可访问方法(inaccessible
methods)”来称呼这么些未定义或不可知的类属性或方式。

作用:当调用当前环境下未定义或不可见的类属性或艺术时,重载方法会被调用。前边将采纳”不可访问属性(inaccessible properties)”和”不可访问方法(inaccessible
methods)”来称呼那一个未定义或不可见的类属性或措施。

注意:全部的重载方法都必须被声称为 public

注意:全部的重载方法都不可能不被声称为 public

 

 

2.__get、__set、__isset、__unset、__call、__callStatic
 //为何放一块吧?

2.__get、__set、__isset、__unset、__call、__callStatic
 //为何放一块吗?

__set当给不可访问或不设有属性赋值时被调用
__get读取不可访问或不存在属性时被调用

__set当给不可访问或不设有属性赋值时被调用
__get读取不可访问或不存在属性时被调用

__isset对不可访问或不设有的质量调用isset()或empty()时被调用
__unset对不可访问或不设有的属性进行unset时被调用

__isset对不可访问或不设有的性格调用isset()或empty()时被调用
__unset对不可访问或不设有的习性实行unset时被调用

__call调用不可访问或不设有的法马时被调用
__callStatic调用不可访问或不存在的静态方法时被调用

__call调用不可访问或不设有的不二法门时被调用
__callStatic调用不可访问或不存在的静态方法时被调用

 

 

Example #1
使用 __get()__set()__isset() 和 __unset() 进行品质重载

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 '$name' to '$value'\n";
        $this->data[$name] = $value;
    }


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

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



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

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

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


echo "<pre>\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's experiment with the private property named 'hidden':\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";
?>
<?php
class PropertyTest {
     /**  被重载的数据保存在此  */
    private $data = array();


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

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

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


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

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



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

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

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


echo "<pre>\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's experiment with the private property named 'hidden':\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时被调用,当您不须求保留大目的的全部数据时很有用

5.__sleep,__wakeup
__sleep当使用serialize时被调用,当你不须求保留大目的的持有数据时很有用

__wakeup当使用unserialize时被调用,可用以做些对象的开头化操作

__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('server', 'username', 'password', 'db');
    }

    public function __wakeup()
    {
        $this->connect();
    }
}
?>
<?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('server', 'username', 'password', 'db');
    }

    public function __wakeup()
    {
        $this->connect();
    }
}
?>

 

 

6.__clone
展开对象clone时被调用,用来调动指标的仿造行为

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);

?>
<?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
当三个类被转换来字符串时被调用

7.__toString
当3个类被转换来字符串时被调用

<?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);

?>
<?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的重回值。

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['var1'];
        $obj->var2 = $an_array['var2'];
        return $obj;
    }
}

$a = new A;
$a->var1 = 5;
$a->var2 = 'foo';

eval('$b = ' . var_export($a, true) . ';'); // $b = A::__set_state(array(
                                            //    'var1' => 5,
                                            //    'var2' => 'foo',
                                            // ));
var_dump($b);

?>
<?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['var1'];
        $obj->var2 = $an_array['var2'];
        return $obj;
    }
}

$a = new A;
$a->var1 = 5;
$a->var2 = 'foo';

eval('$b = ' . var_export($a, true) . ';'); // $b = A::__set_state(array(
                                            //    'var1' => 5,
                                            //    'var2' => 'foo',
                                            // ));
var_dump($b);

?>

 

 

enmmm,笔者清楚你记不住的。。这么多什么人记得住啊,好烦啊、、、别,那还没讲完呢,越多格局请参考PHP的合法文书档案:http://php.net/manual/zh/,别打我,你自己去看吧,毕竟好东西还是要众乐乐的!了;另外这些代码你最好再编译器中运行一边,不然看完你估计和没看一个样2333
····  

enmmm,笔者领会您记不住的。。这么多什么人记得住啊,好烦啊、、、别,那还没讲完呢,更加多形式请参见PHP的法定文书档案:http://php.net/manual/zh/,别打我,你自己去看吧,毕竟好东西还是要众乐乐的!了;另外这些代码你最好再编译器中运行一边,不然看完你估计和没看一个样2333
····  

笔者原创、转载请评释出处

作者原创、转载请注解出处

相关文章