Home >Backend Development >PHP Tutorial >A preliminary exploration of PHP magic functions (1)_PHP tutorial

A preliminary exploration of PHP magic functions (1)_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:24:36801browse

Php5.0 has provided us with many object-oriented features since its release, especially many easy-to-use magic methods. These magic methods allow us to simplify our coding and better design our system. Today we will learn about the magic methods provided by php5.0.

1.__construct() When instantiating an object, this method of the object is first called.

class Test {  function __construct()  {   echo "before";  } } $t = new Test();

The output is:

start
We know that the php5 object model and the function with the same class name are the constructors of the class, so if we define both For constructors and __construct() methods, php5 will call the constructor by default instead of calling the __construct() function, so __construct() serves as the default constructor of the class

2.__destruct () This method is called when an object is deleted or when the object operation terminates.

class Test {  function __destruct()  {   echo "end";  } } $t = new Test();将会输出end

We can perform operations such as releasing resources at the end of the object operation

3.__get() When trying to read an object that does not exist attribute is called.

If you try to read a property that does not exist in an object, PHP will give an error message. If we add the __get method to the class, we can use this function to implement various operations similar to reflection in Java.

class Test {  public function __get($key)  {   echo $key . " 不存在";  } } $t = new Test(); echo $t->name; 就会输出:name 不存在

4.__set() is called when trying to write a value to a property that does not exist.

class Test {  public function __set($key,$value)  {   echo '对'.$key . "附值".$value;  } } $t = new Test(); $t->name = "aninggo"; 就会输出:对 name 附值 aninggo

5.__call() This method is called when trying to call a method that does not exist on the object.

class Test {  public function __call($Key, $Args)  {   echo "您要调用的 {$Key} 方法不存在。你传入的参数是:" . print_r($Args, true);  } } $t = new Test(); $t->getName(aning,go);

The program will output:
The getName method you want to call does not exist. The parameters are: Array
(
[0] => aning
[1] => go
)

6.__toString() when printing an object Called

This method is similar to java's toString method. When we print the object directly, this function is called back.

class Test {  public function __toString()  {   return "打印 Test";  } } $t = new Test(); echo $t; 

When running echo $t; , $t->__toString(); will be called to output
Print Test

7.__clone() When the object is cloned, it will be called

class Test {  public function __clone()  {   echo "我被复制了!";  } }$t = new Test(); $t1 = clone $t;程序输出:我被克隆了!

1

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446751.htmlTechArticlePhp5.0 has provided us with many object-oriented features since its release, especially many easy-to-use features. Magic methods that allow us to simplify our coding and make it better...
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