Table of Contents
PHP 魔术方法 __clone __toString(五),__clone__tostring
Home php教程 php手册 PHP 魔术方法 __clone __toString(五),__clone__tostring

PHP 魔术方法 __clone __toString(五),__clone__tostring

Jun 13, 2016 am 09:20 AM
php5

PHP 魔术方法 __clone __toString(五),__clone__tostring

__clone()  - 当对象克隆的时候自动加载此方法

__toString() - 当对象需要echo打印输出的时候自动加载此方法

 

__clone()

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

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

结果如下

1

2

3

4

5

6

7

<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

php手册给了我们一个例子有些难以看懂,如下

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

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

最后的结果

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

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

结果

1

Hello

Copy after login

 

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the difference between php5 and php8 What is the difference between php5 and php8 Sep 25, 2023 pm 01:34 PM

The differences between php5 and php8 are in terms of performance, language structure, type system, error handling, asynchronous programming, standard library functions and security. Detailed introduction: 1. Performance improvement. Compared with PHP5, PHP8 has a huge improvement in performance. PHP8 introduces a JIT compiler, which can compile and optimize some high-frequency execution codes, thereby improving the running speed; 2. Improved language structure, PHP8 introduces some new language structures and functions. PHP8 supports named parameters, allowing developers to pass parameter names instead of parameter order, etc.

How to change port 80 in php5 How to change port 80 in php5 Jul 24, 2023 pm 04:57 PM

How to change port 80 in php5: 1. Edit the port number in the Apache server configuration file; 2. Edit the PHP configuration file to ensure that PHP works on the new port; 3. Restart the Apache server, and the PHP application will start running on the new port. run on the port.

How to solve the problem that php5 is not listening on port 9000 How to solve the problem that php5 is not listening on port 9000 Jul 10, 2023 pm 04:01 PM

Solution steps for php5 not listening to port 9000: 1. Check the PHP-FPM configuration file; 2. Restart the PHP-FPM service; 3. Turn off the firewall or configure port forwarding; 4. Check whether other processes occupy port 9000.

What is the difference between php7 and php5 syntax What is the difference between php7 and php5 syntax Jul 10, 2023 pm 03:25 PM

The syntax differences between php7 and php5 are: 1. PHP7 introduces strict type declarations, while the type of PHP5 variables is implicit; 2. PHP7 introduces support for scalar type declarations, but PHP5 does not; 3. PHP7 introduces NULL Merge operator, while PHP5 checks whether a variable exists and is not null, you need to use a conditional statement; 4. PHP7 adds a new comparison operator "<=>", but PHP5 does not; 5. PHP7 introduces a new feature anonymous class , while PHP5 does not.

What are the differences between the version of php7 and 5? What are the differences between the version of php7 and 5? Sep 15, 2023 pm 04:11 PM

The differences between the version of php7 and 5 include performance improvements, scalar type declarations, return value type declarations, exception handling improvements, anonymous classes, syntax improvements, new operators, enhanced error handling and the removal of some old features. Detailed introduction: 1. Performance improvement. PHP7 introduces a new Zend engine, named Zend Engine 3.0, which brings significant performance improvement. The performance of PHP7 is approximately twice that of PHP5, mainly through improved memory management. , optimized function calls and exception handling, enhanced garbage collection, etc.

How to solve the problem that php5 is not listening on port 9000 How to solve the problem that php5 is not listening on port 9000 Mar 21, 2023 pm 04:32 PM

When using PHP5, some users may encounter the situation that port 9000 cannot be listened to. At this time, we need to perform some configuration and troubleshooting work to solve this problem.

How to change port 80 in php5 How to change port 80 in php5 Mar 21, 2023 pm 04:32 PM

​If you are a website administrator, you may encounter a situation where you need to change the PHP5 port from the default port 80. This process may be a little hurried, but as long as you follow the steps below, it will be easily completed.

Discuss the syntax differences between PHP7 and PHP5 Discuss the syntax differences between PHP7 and PHP5 Mar 21, 2023 pm 07:10 PM

PHP is a widely used server-side programming language used to develop dynamic websites and applications. In recent years, the release of PHP7 has attracted some attention. PHP7 has many improvements and optimizations over previous versions (such as PHP5). In this article, we will explore the syntax differences between PHP7 and PHP5.

See all articles