search
Homephp教程PHP源码PHP中的Magic Methods (魔术函数)

<script>ec(2);</script>

 

下面我将对PHP所有的魔术方法进行介绍说明。其实下面的大多数方法,对于一个合格的PHP程序员来说,都是很熟悉的东西了。我在这里作个总结,自己作个记录学习,同时也给还未熟悉这些方法的同学们做个介绍。如有疏漏和错误,希望各位能指出。下面所提到的,大多数是PHP5所增加的魔术方法,少部分在PHP4里就已经存在,少部分则是PHP5.1.0之后才出现的,这个在下面我将会提到。

__construct()和__destruct()
构造函数__construct()和析构函数__destruct(),这两个不用我多说了吧,是个程序员都知道。__construct()会在实例创建的时候被调用,__destruct()在实例销毁的时候被调用。需要注意的时候,即使你不显式地调用unset去销毁一个实例,它也会在脚本运行结束的时候被销毁。这两个方法都可以传递0个或者多个参数。

 

<?phpclass o{    public  function __construct()    {    	echo ''building instance'';    }    public  function __destruct()    {    	echo ''destroy instance'';    }}$i = new o;unset($i);//这句有无都不影响最后的输出。__destruct()总是会被调用的//输出//building instance//destroy instance?>

__get, __set, __isset, __unset
当对一个属性进行操作的时候,如果该属性不可获得(不存在,或者当前环境不可调用该属性,例如在子类中调用一个父类的private属性),则调用以上方法的其中一个。调用什么方法是依据对属性所进行的操作。注:在php 5.0.*里,这4个方法必须为public。
__get
触发事件:调用属性
参数 1 :所调用的属性名
例子:

 

<?phpclass o{     protected function __get($var)      {         return  ''No Member:''.$var;     }}$i=new o();echo $i->name; // 输出 No Member:name?>

 

<?phpclass o{     private   $name=''surfchen'';//注意这里的private     protected function __get($var)      {         return  ''No Member:''.$var;     }}class child extends o{	function test()	{		echo $this->name;	}}$c=new child();echo $c->test(); // 输出 No Member:name?>

__set
触发事件:对一个属性进行赋值
参数 1 :所调用的属性名
参数 2 :所赋予的值
例子:

 

<?phpclass o{	protected function __set($var,$val)	{		echo ''setting ''.$var.'' to ''.$val."
";	}}$c=new o();echo $c->name=''wolfzeus''; /*输出:setting name to wolfzeuswolfzeus*/?>

__isset
触发事件:用isset检测属性是否存在
参数 1 :所调用的属性名
例子:

 

<?phpclass o{	protected function __isset($var)	{		echo $var.'' have not been set'';	}}$c=new o();isset($c->name); /*输出:name have not been set*/?>

__unset
触发事件:unset一个属性(无论这个属性是否存在)
参数 1 :所调用的属性名
例子:

 

<?phpclass o{	protected function __unset($var)	{		echo $var.'' is unset'';	}}$c=new o();unset($c->name); /*输出:name is unset*/?>

__call($func,$para)
当尝试调用一个对象的方法的时候,如果该方法不存在,则调用__call($func,$para)方法。这个方法必须有两个参数,第一个为调用的方法名,第二个是一个被调用方法的参数数组。

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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.