PHP的魔术方法学习
__set_state()
var_export可以把一个集合导出为一串字符串,这些字符串是一些可执行的PHP代码。在php5.1.0的面向对象里,引入了一个叫__set_state的静态方法来使得var_export支持对象实例的导出。当用var_export导出一个实例的时候,所导出字符串里,是调用这个静态方法的代码。这个方法有一个参数,为包含所导出的实例的所有成员属性的一个数组。有点抽象,看下面的例子吧。
$v) { $obj->$k = $v; } return $obj; }}$i = new o;$i->age = 21;eval(’$b = ’.var_export($i,true).’;’);//这里的true表示var_export返回所导出的字符串,而不是把它打印出来。print_r($b);/*输出:stdClass Object( [skill] => php [age] => 21)*/?>
__clone()
在php5里,对象间的赋值总是以地址引用来传递的。例如下面这个例子,将会输出66,而不是55.
age = 66;echo $i2->age;?>
如果要以实际值来传递,则需要用到clone关键词。
age = 66;echo $i2->age;//输出55?>
但是,这里clone的只是$i这个实例。如果$i的某个成员属性也是个实例,那么这个成员属性还是会以引用方法被传递到$i2的。例如下面这个例子:
sub=new o2;$i2 = clone $i;$i->sub->p=5;echo $i2->sub->p;?>
最后输出的是5,而不是1。也就是说,$i和$i2虽然不是指向的虽然不是同一个实例,但是它们的成员属性$sub却是指向同一个实例。这时候,我们必须借助__clone这个方法来对$sub进行复制。在o类里,加入__clone()方法。如下:
sub=clone $this->sub; }}//......?>
这样,在echo $i2->sub->p;的时候,输出的就是传递时候的值1了。
__autoload()
当创建一个实例化的时候,如果对应的类不存在,__autoload()将会被执行,这个函数有一个参数,为所要创建的实例对应的类名。在下面的例子里,当创建一个test类的实例的时候,如果/home/surfchen/project/auto.php存在,则require这个文件,否则打印一个Class test Not Found的错误,并中止当前脚本。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
