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?>
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->test(); // 输出 No Member:name?>
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*/?>
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*/?>
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*/?>
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 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)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months 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)

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

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

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

In today's era of rapid technological development, programming languages ​​are springing up like mushrooms after a rain. One of the languages ​​that has attracted much attention is the Go language, which is loved by many developers for its simplicity, efficiency, concurrency safety and other features. The Go language is known for its strong ecosystem with many excellent open source projects. This article will introduce five selected Go language open source projects and lead readers to explore the world of Go language open source projects. KubernetesKubernetes is an open source container orchestration engine for automated

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

&quot;Go Language Development Essentials: 5 Popular Framework Recommendations&quot; As a fast and efficient programming language, Go language is favored by more and more developers. In order to improve development efficiency and optimize code structure, many developers choose to use frameworks to quickly build applications. In the world of Go language, there are many excellent frameworks to choose from. This article will introduce 5 popular Go language frameworks and provide specific code examples to help readers better understand and use these frameworks. 1.GinGin is a lightweight web framework with fast

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 PHP is a widely used server-side scripting language, which is widely used in web development. The echo keyword is a method used to output content in PHP. This article will introduce in detail the function and use of the echo keyword. Function: The main function of the echo keyword is to output content to the browser. In web development, we need to dynamically present data to the front-end page. At this time, we can use the echo keyword to output the data to the page. e

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

In this article, we will learn about enumerate() function and the purpose of “enumerate()” function in Python. What is the enumerate() function? Python's enumerate() function accepts a data collection as a parameter and returns an enumeration object. Enumeration objects are returned as key-value pairs. The key is the index corresponding to each item, and the value is the items. Syntax enumerate(iterable,start) Parameters iterable - The passed in data collection can be returned as an enumeration object, called iterablestart - As the name suggests, the starting index of the enumeration object is defined by start. if we ignore

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

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 is a popular PHP framework that is highly scalable and efficient. It provides many powerful tools and libraries that allow developers to quickly build high-quality web applications. Among them, LaravelEcho and Pusher are two very important tools through which WebSockets communication can be easily implemented. This article will detail how to use these two tools in Laravel applications. What are WebSockets? WebSockets

Detailed explanation of the role and function of the MySQL.proc table Detailed explanation of the role and function of the MySQL.proc table Mar 16, 2024 am 09:03 AM

Detailed explanation of the role and function of the MySQL.proc table. MySQL is a popular relational database management system. When developers use MySQL, they often involve the creation and management of stored procedures (StoredProcedure). The MySQL.proc table is a very important system table. It stores information related to all stored procedures in the database, including the name, definition, parameters, etc. of the stored procedures. In this article, we will explain in detail the role and functionality of the MySQL.proc table

See all articles