Home php教程 PHP源码 PHP中的Magic Methods (魔术函数)

PHP中的Magic Methods (魔术函数)

Jun 08, 2016 pm 05:31 PM
echo function name unset

<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?>
Copy after login

__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?&gt;
Copy after login

 

<?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-&gt;test(); // 输出 No Member:name?&gt;
Copy after login

__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*/?&gt;
Copy after login

__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*/?&gt;
Copy after login

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

 

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

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

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 Article

Hot Article

Hot Article Tags

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)

What does function mean? What does function mean? Aug 04, 2023 am 10:33 AM

What does function mean?

Five selected Go language open source projects to take you to explore the technology world Five selected Go language open source projects to take you to explore the technology world Jan 30, 2024 am 09:08 AM

Five selected Go language open source projects to take you to explore the technology world

Go language development essentials: 5 popular framework recommendations Go language development essentials: 5 popular framework recommendations Mar 24, 2024 pm 01:15 PM

Go language development essentials: 5 popular framework recommendations

Implementing distributed task scheduling using Golang's web framework Echo framework Implementing distributed task scheduling using Golang's web framework Echo framework Jun 24, 2023 am 11:49 AM

Implementing distributed task scheduling using Golang's web framework Echo framework

Laravel development: How to implement WebSockets communication using Laravel Echo and Pusher? Laravel development: How to implement WebSockets communication using Laravel Echo and Pusher? Jun 13, 2023 pm 05:01 PM

Laravel development: How to implement WebSockets communication using Laravel Echo and Pusher?

Detailed explanation of the role and usage of the echo keyword in PHP Detailed explanation of the role and usage of the echo keyword in PHP Jun 28, 2023 pm 08:12 PM

Detailed explanation of the role and usage of the echo keyword in PHP

What is the purpose of the 'enumerate()' function in Python? What is the purpose of the 'enumerate()' function in Python? Sep 01, 2023 am 11:29 AM

What is the purpose of the 'enumerate()' function in Python?

Explore the Go language framework: 5 choices not to be missed! Explore the Go language framework: 5 choices not to be missed! Feb 19, 2024 pm 02:29 PM

Explore the Go language framework: 5 choices not to be missed!

See all articles