What are the common magic methods in PHP?

Source: Internet
Author: User
Tags autoload php class
This article mainly introduces the magic methods commonly used in PHP related data. Has a good reference value. Let's take a look at the little series.

We often use magic methods in PHP, such as the construction method, the destructor method and so on magic variables, the following summarizes some common magic variables:

Construct (), destruct (), Clone (), AutoLoad (), ToString (), Invoke (), set (), Get (), unset (), Isset (), call (), callstatic;

1,constuct () constructor

After you instantiate an object, you will initialize the object's properties! When instantiating an object, the PHP system automatically calls a method called construct (), and we call this method "construction method"!

Where the construction method can have no parameters!

<?phpclass Stu {public $stu _name; public $stu _age;//constructor method, the public function construct ($name, $age) is automatically invoked when instantiated  $this- >stu_name = $name;  $this->stu_age = $age; }}//instantiates stu object $stu = new Stu (' Monkey King ', ' 500 ');

2.destruct () destructor method

And the construction method is a pair, the construction method is automatically called by the system when an object is "born", and the destructor is automatically called by the system when an object "disappears"!

The name of the destructor method is called destruct (); Note that there can be no parameters!

<?php//destructor Public Function destruct () {}?>

Note: The destructor method is called before the object is destroyed! 

Role:

The destructor method is generally used to release the extra resources that the object occupies, rather than destroying the object itself!

3,clone () clone

Trigger time: The task of initializing a new object when cloning an object

The Clone method initializes the new object when the object is cloned public function clone () {  $this->is_clone=true;}

Thinking:

Is the cloned object still an instance of the student class?

We can use the instanceof operator to judge!

Instanceof is used to determine whether an object is an instance of a class!

Requires two operands, preceded by an object variable, followed by a class name! Returns a Boolean value!

var_dump($stu instanceof Stu);   //返回bool(true)

4.autoload () Auto Load class

Called automatically when a non-existent class is called!

<?php/*** Auto Load class * requires what class AutoLoad automatically calls the required file **/public function AutoLoad ($class _name) {require_once]. /model/{$class _name}.class.php ";} ?>

5.tostring ()

Called automatically when an object is used as a string

6.invoke ()

Called automatically when the object is used as a function

7,set ()

Called automatically when a value is assigned to an unreachable property (for example, calling a private property outside of a class)

8.get ()

Called automatically when the value of an unreachable property is obtained

9.unset ()

Automatically called when destroying an unreachable property

10.isset ()

Called automatically when you determine if an unreachable property exists

11.call ()

triggered automatically when calling a normal method that cannot be accessed

<?php class stu{Public Function call () {echo "Error";}} $stu = new Stu ();//At this time the Stu class does not have a show () method, which triggers the call () function $stu->show ();

12.callststic ()

Automatically triggered when access is not available through static methods

<?php class stu{public static function Callstatic () {  echo "error";}} $stu = new Stu ();//At this time the Stu class does not have a show () method, which triggers the call () function $stu::show ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.