search
HomeBackend DevelopmentPHP Tutorialphp的幻术函数和魔术常量

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)初识魔术方法
Php5.0发布以来为我们提供了很多面向对象 的特性,尤其是为我们提供了好多易用的魔术方法,这些魔术方法可以让我们简化我们的编码,更好的设计我们的系统。今天我们就来认识下php5.0给我们提供的魔术方法。
1,__construct() 当实例化一个对象的时候,这个对象的这个方法首先被调用。

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

输出是:
start


class Test
{
    function Test(){
   echo "end2";
}
function __construct(){
   echo "end";
}
}
$t = new Test();
//输出end
?>
我们知道php5对象模型 和类名相同 的函数 是类的构造函数 ,那么如果我们同时定义构造函数 和__construct()方法的话,php5会默认调用构造函数 而不会调用同类名函数 ,所以__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;
程序输出:
我被克隆了!
__sleep 和 __wakeup
串行化serialize可以把变量包括对象,转化成连续bytes数据. 你可以将串行化后的变量存在一个文件里或在网络上传输. 然后再反串行化还原为原来的数据. 你在反串行化类的对象之前定义的类,PHP可以成功地存储其对象的属性和方法. 有时你可能需要一个对象在反串行化后立即执行. 为了这样的目的,PHP会自动寻找__sleep和__wakeup方法.
  当一个对象被串行化,PHP会调用__sleep方法(如果存在的话). 在反串行化一个对象后,PHP 会调用__wakeup方法. 这两个方法都不接受参数. __sleep方法必须返回一个数组,包含需要串行化的属性. PHP会抛弃其它属性的值. 如果没有__sleep方法,PHP将保存所有属性.
  例子6.16显示了如何用__sleep和__wakeup方法来串行化一个对象. Id属性是一个不打算保留在对象中的临时属性. __sleep方法保证在串行化的对象中不包含id属性. 当反串行化一个User对象,__wakeup方法建立id属性的新值. 这个例子被设计成自我保持. 在实际开发中,你可能发现包含资源(如图像或数据流)的对象需要这些方法
Object serialization
CODE: [Copy to clipboard]
--------------------------------------------
class User
{
public $name;
public $id;
function __construct() {
   //give user a unique ID 赋予一个不同的ID
   $this->id = uniqid();
}
function __sleep() {
   //do not serialize this->id 不串行化id
   return(array("name"));
}
function __wakeup() {
   //give user a unique ID
   $this->id = uniqid();
}
}
//create object 建立一个对象
$u = new User;
$u->name = "Leon";
//serialize it 串行化 注意不串行化id属性,id的值被抛弃
$s = serialize($u);
//unserialize it 反串行化 id被重新赋值
$u2 = unserialize($s);
//$u and $u2 have different IDs $u和$u2有不同的ID
print_r($u);
print_r($u2);
?>
__set_state and __invoke
测试代码如下:
class A {
public static function __set_state($args)
{
    $obj=new A();
    foreach($args as $k=>$v){
       $obj->$k = $v;
    }
    return $obj;
}
}

$a = new A;
$a->name = 'cluries';
$a->sex = 'female';
eval('$b = ' . var_export($a, true).';');
print_r($b);
?>
程序输出
object(A)#2 (2) {
         ["name"]=> string(7) "cluries"
         ["sex"]=> string(6) "female"
}
得出以下结论,__set_state作用是用来复制一个对象,并且可以在__set_state中定义在复制对象的时候对复制得到的对象进行一些改变。和__clone不同的是__set_state可以接受参数,__set_state使用起来更加强大!虽然个人觉得这个东西不是很好用= =!

然后再说下__invoke:
手册上有个非常显眼的:Note: This feature is available since PHP 5.3.0.
The __invoke method is called when a script tries to call an object as a function.
__invoke方法将会在代码试图把对象当作函数来使用时候调用?有点稀奇,这个功能有什么用处呢?
然后看下提供的例子:

class CallableClass {
function __invoke($x){
    var_dump($x);
}
}
$obj=new CallableClass;
$obj(5);
var_dump(is_callable($obj));
?>
程序输出:
int(5)
bool(true)
还真是把对象当函数使用…
__autoload

PHP5中有一方法: __autoload() , 简单的说就是类的自动加载;
当你尝试使用一个PHP没有组织到的类, 它会寻找一个__autoload的全局函数. 如果存在这个函数,PHP会用一个参数来调用它,参数即类的名称。
那么简单测试一下。

首先建一个名为”Test_autoload.php”的文件:
/**
* 测试__autoload方法
*
*/
class Test_autoload {
public function __construct () {
   echo " Test_autoload. " ;
}
}
?>
注意类名哦 , 然后随便建个文件重写 __autoload() 方法,这里假设是”test.php”;
/**
* 重写 __autoload方法
*/
function __autoload ( $class ) {
include $class . ' .php ' ;
}
$test = new Test_autoload () ;
unset ( $test ) ;
?>
最后结果为:Test_autoload.
------------------------------------------------
8.顺便介绍下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

1 楼 bardo 2011-04-18  
3。__call()
对象调用某个方法,若方法存在,则直接调用;若不存在,则会去调用__call函数。
4。__get()
读取一个对象的属性时,若属性存在,则直接返回属性值;若不存在,则会调用__get函数。
5。__set()
设置一个对象的属性时,若属性存在,则直接赋值;若不存在,则会调用__set函数。

其实要访问的属生方法都存在,只是私有,也会触发调用。

2 楼 bardo 2011-04-18  
6。__toString()
打印一个对象的时被调用。如echo $obj;或print $obj;

strval($obj)一样也会触发调用。

3 楼 bardo 2011-04-18  
1。__construct()
实例化对象时被调用,当__construct和以类名为函数名的函数同时存在时,__construct将被调用,另一个不被调用。

7。__clone()
克隆对象时被调用。如:$t=new Test();$t1=clone $t;

13。__autoload()
实例化一个对象时,如果对应的类不存在,则该方法被调用。


这三个是运算符触发

一个是new运算符。一个是clone运算符。

4 楼 bardo 2011-04-18  
8。__sleep()
serialize之前被调用。若对象比较大,想删减一点东东再序列化,可考虑一下此函数。
9。__wakeup()
unserialize时被调用,做些对象的初始化工作。

。__sleep()
不限于删减。有__call函数的类,如果没有__sleep() 无法完成序列化的。

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's Purpose: Building Dynamic WebsitesPHP's Purpose: Building Dynamic WebsitesApr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP: Handling Databases and Server-Side LogicPHP: Handling Databases and Server-Side LogicApr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

How do you prevent SQL Injection in PHP? (Prepared statements, PDO)How do you prevent SQL Injection in PHP? (Prepared statements, PDO)Apr 15, 2025 am 12:15 AM

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python: Code Examples and ComparisonPHP and Python: Code Examples and ComparisonApr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP in Action: Real-World Examples and ApplicationsPHP in Action: Real-World Examples and ApplicationsApr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: Creating Interactive Web Content with EasePHP: Creating Interactive Web Content with EaseApr 14, 2025 am 12:15 AM

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python: Comparing Two Popular Programming LanguagesPHP and Python: Comparing Two Popular Programming LanguagesApr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

The Enduring Relevance of PHP: Is It Still Alive?The Enduring Relevance of PHP: Is It Still Alive?Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

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)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

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.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool