Home php教程 php手册 PHP的魔术方法学习

PHP的魔术方法学习

Jun 13, 2016 am 11:35 AM
export php set state var for Can string study Export Bundle method of gather magic

__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的错误,并中止当前脚本。

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

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

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

See all articles