Summary of magic methods for php face objects

Source: Internet
Author: User


1. _ construct ()
The instantiated object is automatically called. When _ construct and a function with the class name exist at the same time, _ construct is called, and the other one does not call it back.
The class name function is the old version of the constructor.
2. _ destruct ()
When an object or object is deleted, it is called.
3. _ call ()
Object to call a method. If the method does not exist, call the _ call method.
4. _ get ()
Read an object property. If the object property is private, it will be called.
5. _ set ()
When an object property is assigned a value, if the property is private, it will be called.
6. _ toString ()
When printing an object, it is called.
7. _ clone ()
Called when an object is cloned, for example, $ a = new test (); $ a1 = clone $;
8. _ sleep ()
Serialize is called before. If the object is large, you can use it to delete something before serialization.
9. _ wakeup ()
Unserialize is called to initialize objects.
10. _ isset ()
Checks whether an object's attribute exists. If the detected attribute is private, it is called.
11. _ unset ()
When deleting an object property, if the deleted object property is private, it will be called.
12. _ set_state ()
Called when var_export is called. Use the return value of _ set_state as the return value of var_export.
13. _ autoload ()
When instantiating an object, if the corresponding class does not exist, the method will be deprecated.


Php books = php web development

The code is as follows: Copy code


<? Php
Class BookStore {
Private $ p = array ();

Function _ set ($ name, $ value ){
Print "set ::::{$ name }:{$ value} <br/> ";
$ This-> p [$ name] = $ value;
 }
Function _ get ($ name ){
Print "get ::::{$ name} <br/> ";
// Array_key_exists check whether the given key name or index exists in the array
Return array_key_exists ($ name, $ this-> p )? $ This-> p [$ name]: null;
 }
}
$ Computer = new BookStore ();
$ Computer-> php = 'php web development Daquan ';
Echo "php books =". $ computer-> php;
?>


(2) _ autoload (): are you still worried about calling too many classes or functions? Are you still worried about writing require () and include () n times ()
However, if you know the _ autoload () method, you don't need to do so. Here I will give you a reference:
MyClass. Php file:

The code is as follows: Copy code


<? Php
Class MyClass {
Function printWeb (){
Echo "the website you are visiting is www.111cn.net ";
  }
 }
?>

General. inc. Php file


<? Php
Function _ autoload ($ class_name ){
$ File (dirname (_ FILE _). "/libs/classes/$ class_name.php ";
If (! File_exists ($ file )){
Return false;
} Else {
Require_once ($ file );
 }
}
?>

Main. php


<? Php
Require ("general. inc. php ");
$ Obj = new Myclass ();
If (is_object ($ obj )){
$ Obj-> printWeb ();
} Else {
Echo "class file not transferred ";
}
?>


Running result:
The website you are visiting is www.111cn.net.

Of course, there are still many magic methods, such as _ isset () ,__ unset () ,__ toString () ,__ clone ().

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.