Introduction to PHP's magic functions and magic constants_PHP Tutorial

WBOY
Release: 2016-07-13 10:33:23
Original
719 people have browsed it

Magic function

  1. __construct()
  2. Called when instantiating an object. When __construct and a function with the class name and function name exist at the same time, __construct will be called and the other will not be called.

  3. __destruct()
  4. Called when an object is deleted or the object operation terminates.

  5. __call()
  6. The object calls a method, If the method exists, call it directly; If it does not exist, the __call function will be called.

  7. __get()
  8. When reading the properties of an object, If the attribute exists, the attribute value is returned directly; If it does not exist, the __get function will be called.

  9. __set()
  10. When setting the properties of an object, If the attribute exists, assign it directly; If it does not exist, the __set function will be called.

  11. __toString()
  12. Called when printing an object. Such as echo $obj; or print $obj;

  13. __clone()
  14. Called when cloning an object. For example: $t=new Test();$t1=clone $t;

  15. __sleep()
  16. serialize was called before. If the object is relatively large and you want to delete a little bit before serializing it, you can consider this function.

  17. __wakeup()
  18. Called when unserialize is used to do some object initialization work.

  19. __isset()
  20. Called when checking whether a property of an object exists. Such as: isset($c->name).

  21. __unset()
  22. Called when unsetting a property of an object. For example: unset($c->name).

  23. __set_state()
  24. Called when var_export is called. Use the return value of __set_state as the return value of var_export.

  25. __autoload()
  26. When instantiating an object, if the corresponding class does not exist, this method is called.

Magic Constants

  1. __LINE__
  2. Returns the current line number in the file.

  3. __FILE__
  4. Returns the full path and file name of the file. If used in an include file, returns the include file name. As of PHP 4.0.2, __FILE__ always contains an absolute path, while versions before that sometimes contained a relative path.

  5. __FUNCTION__
  6. Return function name (new in PHP 4.3.0). Since PHP 5 this constant returns the name of the function as it was defined (case sensitive). In PHP 4 this value is always lowercase.

  7. __CLASS__
  8. Returns the name of the class (new in PHP 4.3.0). Since PHP 5 this constant returns the name of the class when it was defined (case sensitive). In PHP 4 this value is always lowercase.

  9. __METHOD__
  10. Returns the method name of the class (newly added in PHP 5.0.0). Returns the name of the method as it was defined (case-sensitive).

First introduction to magic methods

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

__get() is called when trying to read a property that does not exist.

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

will output: name does not exist

__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";
Copy after login

will output: Attach value to name aninggo

__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);
Copy after login

The program will output: The getName method you are trying to call does not exist.

The parameters are:

Array
(
[0] => aning
[1] => go
)
Copy after login

__toString() is called when printing an object

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

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

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

__clone() is called when the object is cloned

class Test
{
public function __clone()
{
  echo "我被复制了!";
}
}
$t = new Test();
$t1 = clone $t;
Copy after login

Program output: I was copied

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752480.htmlTechArticleThe magic function __construct() is called when instantiating an object. When __construct is combined with the class name and the function name When functions exist at the same time, __construct will be called and the other will not. __destr...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!