search
HomeBackend DevelopmentPHP Tutorial PHP幻术函数执行时间和顺序解析

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() 当实例化一个对象的时候,这个对象的这个方法首先被调用。

class Test 
{ 
 function __construct() 
 { 
  echo "before"; 
 } 
} 
$t = new Test();

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

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

class Test 
{ 
 function __destruct() 
 { 
  echo "end"; 
 } 
} 
$t = new Test();
将会输出
end

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

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

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

class Test 
{ 
 public function __get($key) 
 { 
  echo $key . " 不存在"; 
 } 
} 

$t = new Test(); 
echo $t->name; 

就会输出:
name 不存在

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

class Test 
{ 
 public function __set($key,$value) 
 { 
  echo '对'.$key . "附值".$value; 
 } 
} 


$t = new Test(); 
$t->name = "aninggo"; 

就会输出:
对 name 附值 aninggo

?

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

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

$t = new Test(); 
$t->getName(aning,go);

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

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

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

class Test 
{ 
 public function __toString() 
 { 
  return "打印 Test"; 
 } 
} 


$t = new Test(); 

echo $t; 

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

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

class Test 
{ 

 public function __clone() 
 { 
  echo "我被复制了!"; 
 } 
}

$t = new Test(); 
$t1 = clone $t;

程序输出:我被克隆了!

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

class Test 
{ 

 function foo() {
        return "foo! ";
    }

} 

runkit_method_rename(
    'Test', //类名
    'foo',//实际调用的函数
    'bar'//显示调用的函数
);

echo Test::bar();

程序将输出
 
foo!

(2) runkit_method_add

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

class Test 
{ 

 function foo() {
        return "foo! ";
    }

} 

runkit_method_add(
    Test,  //类名
    'add', //新函数名
    '$num1, $num2',//传入参数
    'return $num1 + $num2;',//执行的代码
    RUNKIT_ACC_PUBLIC
);

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

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

class Foo {
    function example() {
        return "foo! ";
    }
}

class Bar {
    //空类
}

//执行拷贝
runkit_method_copy('Bar', 'baz', 'Foo', 'example');

//执行拷贝后的函数
echo Bar::baz();

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

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

(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
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
PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

How does PHP handle object cloning (clone keyword) and the __clone magic method?How does PHP handle object cloning (clone keyword) and the __clone magic method?Apr 17, 2025 am 12:24 AM

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP vs. Python: Use Cases and ApplicationsPHP vs. Python: Use Cases and ApplicationsApr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

See all articles

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)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools