Home > Backend Development > PHP Tutorial > PHP magic method: __clone __toString

PHP magic method: __clone __toString

WBOY
Release: 2016-08-08 09:33:11
Original
1157 people have browsed it

From php5 and later versions, classes can use magic methods. PHP stipulates that methods starting with two underscores (__) are reserved as magic methods, so it is recommended that function names do not start with __ unless it is to overload existing magic methods.

The existing magic methods in PHP include __construct, __destruct, __call, __get, __set, __isset, __unset, __sleep, __wakeup, __toString, __set_state and __clone.

Let’s take a look at two magic methods: __clone() and __toString().

__clone() - This method is automatically loaded when the object is cloned

__toString() - This method is automatically loaded when the object needs echo printout

__clone()

<?php
class example{
    public static $pa;
    public $pb;
    public function __construct(){
        $this->pb = ++self::$pa;
    }
    
    public function __clone(){
        $this->pb = 'no zuo no die';
    }
}

$a = new example;
$b = new example;
$c = clone $b;
$b->pb = 'I Love You So Much!';

echo $a->pb;
echo '<hr/>';
echo $b->pb;
echo '<hr/>';
echo $c->pb;
echo '<hr/>';
echo $b->pb;
?>
Copy after login

The results are as follows

<span>1</span> ------------------------------------------------------------------------------------<span> I Love You So Much! </span>------------------------------------------------------------------------------------<span> no zuo no die /*要不是有__clone()这个魔术方法,这边的结果应该是2*/ </span>------------------------------------------------------------------------------------<span> I Love You So Much!</span>
Copy after login

The PHP manual gives us an example that is somewhat difficult to understand, as follows:

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

?>
Copy after login

final result

Original <span>Object</span><span>:
MyCloneable </span><span>Object</span><span> (
    [object1] </span>=> SubObject <span>Object</span><span> (
            [instance] </span>=> <span>1</span><span> )

    [object2] </span>=> SubObject <span>Object</span><span> (
            [instance] </span>=> <span>2</span><span> )

)
Cloned </span><span>Object</span><span>:
MyCloneable </span><span>Object</span><span> (
    [object1] </span>=> SubObject <span>Object</span><span> (
            [instance] </span>=> <span>3 <span>/*可能这里比较难以理解,其实就是$obj2当克隆的时候将最后的instance为2的结果克隆,并且再执行SubObject::__clone方法*/</span></span><span> )

    [object2] </span>=> SubObject <span>Object</span><span> (
            [instance] </span>=> <span>2</span><span> )

)</span>
Copy after login

__toString()

<?php
// Declare a simple class
class TestClass
{
    public $foo;

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

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

$class = new TestClass('Hello');
echo $class;
?>
Copy after login

result

Hello
Copy after login

The above introduces the PHP magic method: __clone __toString, including the content of PHP magic method. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template