Home Backend Development PHP Tutorial PHP幻术函数执行时间和顺序解析

PHP幻术函数执行时间和顺序解析

Jun 13, 2016 pm 01:11 PM
class echo function test

PHP魔术函数执行时间和顺序解析

一:魔术函数

1。__construct()
实例化对象时被调用,
当__construct和以类名为函数名的函数同时存在时,__construct将被调用,另一个不被调用。
2。__destruct()
当删除一个对象或对象操作终止时被调用。
3。__call()
对象调用某个方法,
若方法存在,则直接调用;
若不存在,则会去调用__call函数。
4。__get()
读取一个对象的属性时,
若属性存在,则直接返回属性值;
若不存在,则会调用__get函数。
5。__set()
设置一个对象的属性时,
若属性存在,则直接赋值;
若不存在,则会调用__set函数。
6。__toString()
打印或者输出一个对象的时被调用。如echo $obj;或print $obj;
7。__clone()
克隆对象时被调用。如:$t=new Test();$t1=clone $t; 8。__sleep()
serialize之前被调用。若对象比较大,想删减一点东东再序列化,可考虑一下此函数。
9。__wakeup()
unserialize时被调用,做些对象的初始化工作。
10。__isset()
检测一个对象的属性是否存在时被调用。如:isset($c->name)。
11。__unset()
unset一个对象的属性时被调用。如:unset($c->name)。
12。__set_state()
调用var_export时,被调用。用__set_state的返回值做为var_export的返回值。
13。__autoload()
实例化一个对象时,如果对应的类不存在,则该方法被调用。
二:魔术常量
1。__LINE__
返回文件中的当前行号。
2。__FILE__
返回文件的完整路径和文件名。如果用在包含文件中,则返回包含文件名。自 PHP 4.0.2 起,__FILE__ 总是包含一个绝对路径,而在此之前的版本有时会包含一个相对路径。
3。__FUNCTION__
返回函数名称(PHP 4.3.0 新加)。自 PHP 5 起本常量返回该函数被定义时的名字(区分大小写)。在 PHP 4 中该值总是小写字母的。
4。__CLASS__
返回类的名称(PHP 4.3.0 新加)。自 PHP 5 起本常量返回该类被定义时的名字(区分大小写)。在 PHP 4 中该值总是小写字母的。
5。__METHOD__
返回类的方法名(PHP 5.0.0 新加)。返回该方法被定义时的名字(区分大小写)。

三:魔术函数实例

1.__construct() 当实例化一个对象的时候,这个对象的这个方法首先被调用。

1

2

3

4

5

6

7

8

class Test

{

 function __construct()

 {

  echo "before";

 }

}

$t = new Test();

Copy after login

输出是:start
我们知道php5对象模型 和类名相同的函数是类的构造函数,那么如果我们同时定义构造函数和__construct()方法的话,php5会默认调用构造函数而不会调用__construct()函数,所以__construct()作为类的默认的构造函数

2.__destruct() 当删除一个对象或对象操作终止的时候,调用该方法。

1

2

3

4

5

6

7

8

9

10

class Test

{

 function __destruct()

 {

  echo "end";

 }

}

$t = new Test();

将会输出

end

Copy after login

我们就可以在对象操作结束的时候进行释放资源之类的操作

3.__get() 当试图读取一个并不存在的属性的时候被调用。

如果试图读取一个对象并不存在的属性的时候,PHP就会给出错误信息。如果在类里添加__get方法,并且我们可以用这个函数实现类似java中反射的各种操作。

1

2

3

4

5

6

7

8

9

10

11

12

13

class Test

{

 public function __get($key)

 {

  echo $key . " 不存在";

 }

}

 

$t = new Test();

echo $t->name;

 

就会输出:

name 不存在

Copy after login

4.__set() 当试图向一个并不存在的属性写入值的时候被调用。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

class Test

{

 public function __set($key,$value)

 {

  echo '对'.$key . "附值".$value;

 }

}

 

 

$t = new Test();

$t->name = "aninggo";

 

就会输出:

对 name 附值 aninggo

Copy after login

?

5.__call() 当试图调用一个对象并不存在的方法时,调用该方法。

1

2

3

4

5

6

7

8

9

10

class Test

{

 public function __call($Key, $Args)

 {

  echo "您要调用的 {$Key} 方法不存在。你传入的参数是:" . print_r($Args, true);

 }

}

 

$t = new Test();

$t->getName(aning,go);

Copy after login

程序将会输出:
您要调用的 getName 方法不存在。参数是:Array
(
[0] => aning
[1] => go
)

6.__toString() 当打印一个对象的时候被调用

这个方法类似于java的toString方法,当我们直接打印对象的时候回调用这个函数

1

2

3

4

5

6

7

8

9

10

11

12

class Test

{

 public function __toString()

 {

  return "打印 Test";

 }

}

 

 

$t = new Test();

 

echo $t;

Copy after login

运行echo $t;的时候,就会调用$t->__toString();从而输出
打印 Test

7.__clone() 当对象被克隆时,被调用

1

2

3

4

5

6

7

8

9

10

11

12

13

class Test

{

 

 public function __clone()

 {

  echo "我被复制了!";

 }

}

 

$t = new Test();

$t1 = clone $t;

 

程序输出:我被克隆了!

Copy after login

四.顺便介绍下php5中提供的几个非常COOl的实验性函数
(1)runkit_method_rename
这个函数可以动态的改变我们所调用的函数的名字。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

class Test

{

 

 function foo() {

        return "foo! ";

    }

 

}

 

runkit_method_rename(

    'Test', //类名

    'foo',//实际调用的函数

    'bar'//显示调用的函数

);

 

echo Test::bar();

 

程序将输出

 

foo!

Copy after login

(2) runkit_method_add

这个函数可以动态的向类中添加函数

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

class Test

{

 

 function foo() {

        return "foo! ";

    }

 

}

 

runkit_method_add(

    Test,  //类名

    'add', //新函数名

    '$num1, $num2',//传入参数

    'return $num1 + $num2;',//执行的代码

    RUNKIT_ACC_PUBLIC

);

 

// 调用

echo $e->add(12, 4);

Copy after login

(3)runkit_method_copy
可以把A类中的函数拷贝到类B中并对函数重命名

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

class Foo {

    function example() {

        return "foo! ";

    }

}

 

class Bar {

    //空类

}

 

//执行拷贝

runkit_method_copy('Bar', 'baz', 'Foo', 'example');

 

//执行拷贝后的函数

echo Bar::baz();

Copy after login

(4) runkit_method_redefine
动态的修改函数的返回值
这个函数可以让我们轻松的实现对类的MOCK测试!是不是很COOL呢

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

class Example {

    function foo() {

        return "foo! ";

    }

}

 

//创建一个测试对象

$e = new Example();

 

// 在测试对象之前输出

echo "Before: " . $e->foo();

 

// 修改返回值

runkit_method_redefine(

    'Example',

    'foo',

    '',

    'return "bar! ";',

    RUNKIT_ACC_PUBLIC

);

 

// 执行输出

echo "After: " . $e->foo();

Copy after login

(5)runkit_method_remove
这个函数就很简单了,看名字就能看出来了,动态的从类中移除函数

class Test { function foo() { return "foo! "; } function bar() { return "bar! "; } } // 移除foo函数 runkit_method_remove( 'Test', 'foo' ); echo implode(' ', get_class_methods('Test')); 程序输出 bar

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 尊渡假赌尊渡假赌尊渡假赌

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)

Huawei Watch GT 5 smartwatch gets update with new features Huawei Watch GT 5 smartwatch gets update with new features Oct 03, 2024 am 06:25 AM

Huawei is rolling out software version 5.0.0.100(C00M01) for the Watch GT 5 and the Watch GT 5 Prosmartwatchesglobally. These two smartwatches recently launched in Europe, with the standard model arriving as the company’s cheapest model. This Harmony

Tekken\'s Colonel Sanders dream fried by KFC Tekken\'s Colonel Sanders dream fried by KFC Oct 02, 2024 am 06:07 AM

Katsuhiro Harada, the Tekken series director, once seriously tried to bring Colonel Sanders into the iconic fighting game. In an interview with TheGamer, Harada revealed that he pitched the idea to KFC Japan, hoping to add the fast-food legend as a g

First look: Leaked unboxing video of upcoming Anker Zolo 4-port 140W wall charger with display First look: Leaked unboxing video of upcoming Anker Zolo 4-port 140W wall charger with display Oct 01, 2024 am 06:32 AM

Earlier in September 2024, Anker's Zolo 140W charger was leaked, and it was a big deal since it was the first-ever wall charger with a display from the company. Now, a new unboxing video from Xiao Li TV on YouTube gives us a first-hand look at the hi

New Xiaomi Mijia Graphene Oil Heater with HyperOS arrives New Xiaomi Mijia Graphene Oil Heater with HyperOS arrives Oct 02, 2024 pm 09:02 PM

Xiaomi will shortly launch the Mijia Graphene Oil Heater in China. The company recently ran a successful crowdfunding campaign for the smart home product, hosted on its Youpin platform. According to the page, the device has already started to ship to

Samsung Galaxy Z Fold Special Edition revealed to land in late October as conflicting name emerges Samsung Galaxy Z Fold Special Edition revealed to land in late October as conflicting name emerges Oct 01, 2024 am 06:21 AM

The launch of Samsung's long-awaited 'Special Edition' foldable has taken another twist. In recent weeks, rumours about the so-called Galaxy Z Fold Special Edition went rather quiet. Instead, the focus has shifted to the Galaxy S25 series, including

Garmin releases Adventure Racing activity improvements for multiple smartwatches via new update Garmin releases Adventure Racing activity improvements for multiple smartwatches via new update Oct 01, 2024 am 06:40 AM

Garmin is ending the month with a new set of stable updates for its latest high-end smartwatches. To recap, the company released System Software 11.64 to combat high battery drain across the Enduro 3, Fenix E and Fenix 8 (curr. $1,099.99 on Amazon).

Cybertruck FSD reviews praise quick lane switching and full-screen visualizations Cybertruck FSD reviews praise quick lane switching and full-screen visualizations Oct 01, 2024 am 06:16 AM

Tesla is rolling out the latest Full Self-Driving (Supervised) version 12.5.5 and with it comes the promised Cybertruck FSD option at long last, ten months after the pickup went on sale with the feature included in the Foundation Series trim price. F

Manjaro 24.1 \'Xahea\' launches with KDE Plasma 6.1.5, VirtualBox 7.1, and more Manjaro 24.1 \'Xahea\' launches with KDE Plasma 6.1.5, VirtualBox 7.1, and more Oct 02, 2024 am 06:06 AM

With a history of over one decade, Manjaro is regarded as one of the most user-friendly Linux distros suitable for both beginners and power users, being easy to install and use. Mostly developed in Austria, Germany, and France, this Arch-based distro

See all articles