PHP notes (ii)

Source: Internet
Author: User

(i) Automatic loading

Magic Method Summary :
__construct (): constructors , which are automatically called when new objects are initialized, assign initial values to instantiated objects, and so on
__destruct (): destructors , objects are automatically called when they are logged off, can print something to


__get (): As long as the private property of the Access object (both inside and outside) is automatically called, passing the property name that needs to be read, remember to return this property name
__set (): Whenever a private property of an object is modified (both inside and outside), it is called automatically, passing the property name that needs to be modified, and remember to return this property name


__unset (): Automatically called when a private property of an object is deleted, passing in the deleted property name, and then writing again in the function unset function
__isset (): Automatically called when a private property of an object is detected using Isset, passing the detected property name. Returns the Isset property name


 __tostring (): called automatically when using Echo to print an object. Display the content that you want to be realistic, usually write a delimiter variable. Note The return string
 __call (): When calling a function that is not exposed (that is, a private non-existent), it is called automatically, passing in the name of the called function, and an array, the list of arguments in place
 __clone (): When cloning an object using clone, The new cloned object can be assigned an initial value.
 __sleep (): called automatically when an object is serialized. Returns an array whose elements are the properties we need to serialize
 __wakeup (): called automatically when an object is crossdress. Assigns an initial value to the Crossdress property.
 __autoload (): most important. Called Outside the class, and when a class that is not in this document is called, the corresponding class file

  is automatically imported; 1. Automatic loading
 function __autoload ($className {
include strtolower ($className). ". Class.php ";
}
  note :
  This will be when instantiating an object, if the class used is not in the current file, That will automatically call this function. This function is not called if it exists.
  This import for the same class file will only be imported once, once imported, the class has been copied over, equivalent to the class, and then instantiate, do not call him
  Remember that the class files are written to a dedicated folder, and the format must be the same, completely lowercase , especially the class name, must be lowercase, and don't forget
 include strtolower ($className). Class.php "; note format, 2 points must not be forgotten
  This, when instantiated, automatically passes the nonexistent class name as a parameter to __autoload (); and then we turn him strtolower to lowercase, and then we spell the string


2. Serialization and deserialization of objects. also called serialization and crossdress.
Serialize serialization, Unserialize crossdress line
$str =serialize ($lisi); He just converts the object into a string, so you can print it with echo.
echo $str. " <br/> ";
$str 1=unserialize ($STR); Transforms an already serialized object back into an object using the deserialization function.
Var_dump ($str 1);


Scenarios where serialization is used
When the object data needs to be transmitted in the network;
Object data needs to be written in a file, and object data is persisted in the database for a long time.


3. __sleep
When executing the serialization of an object, that is, when calling the Serialize function. Automatically executes the __sleep function, and he needs to return an array
What member properties are written in the array, and those member properties can be serialized. Note that when the attribute is not written in the array, all properties are not serialized. If you do not write
The __sleep function is that all properties are serialized by default.


4. __wakeup
When the execution object crossdress, that is, when the Unserialize function is called. Automatically executes the __weekup function, which he can line up for crossdress
The property is re-assigned value
function __wakeup () {
$this->name= "Wang Wenyan";
}
$b =unserialize ($a);
Echo $b->name; This will print out the Wang Wenyan.


5. Type constraints
Refers to the data type before the variable, to force the type of this variable to be constrained, so that the variable can only receive this type of data, other types cannot receive
function func (array $a) {
Var_dump ($a);
}
Func (["AAA", 1]);
Note: Only arrays and objects can be type constrained, other number, string, etc. are not available in PHP, and other strong languages can

function func (array $a) {
Echo $a->name;
}
Func (New Student ("Mulberry", 15, "schoolgirl"));
Note that if it is a class, it can only be the corresponding class and subclass of the class.
Note: in PHP, data constraints are used only in function parameters, and other places are not available and will be error-bound.
New Student (); This is a direct creation of an anonymous object, which is not assigned to the variable, exists, but I'm not looking for

(ii) abstract class

Serialization of objects
1. Clone and __clone
Reference data type, pass the address, so change one, the other changes.
But cloning an object completely from another object, the two objects are independent and non-intrusive.
$lisi = Clone $zhangsan; The __clone Magic method is called automatically when an external object uses the Clone keyword.
The __clone function, similar to cloning, invokes a constructor that assigns a new value to the property of the new cloned object, and does not write the value of the original cloned object.
The $this in the __clone function refers to the current, new object

2. __tostring function
Echo $lisi, when the object is printed with an output statement such as ECHO, it automatically calls the __tostring function and prints out the return from the function.
3. __call function
$zhangsan->say1 (a); This magic function is called automatically when an undefined function, or a private function, is called.
function __call ($name, $canshu) {
echo "The function you called is undefined";
Var_dump ($name);//print out the first parameter directly
Var_dump ($canshu);//Print out a second parameter directly
The first argument is the name of the function we call, the second argument is the array, and the argument we call this method is passed

(c) interface

"abstract class"
1. What is an interface?
An interface is a specification that provides a set of method combinations that must be implemented by a class that implements an interface.
The interface uses the interface keyword declaration;

Interface inter{}

2. All methods in an interface must be abstract methods.
The abstract method in the interface does not requirenor can you use the abstract modifier

3. The interface cannot declare variables, cannot have attributes, only use Constants!!!
Const NUM = 10;

4. Interface can inherit interface, use extends keyword!
Interfaces use extends to inherit interfaces, nor can they implement multiple commitments.
Interface Int1 extends inter{}

5. Class can implement interface, use implement keyword!
class uses implement implementation interface, can implement multiple interfaces at the same time, multiple interfaces using commas separated;
Class person implements inter,inter2{}

A class implements one or more interfaces, the class must implement all the abstract methods in all interfaces! Unless, this class is an abstract class

"interface differs from abstract class"
1, the way of declaration, the interface uses the interface keyword, abstract class use
2, implementation/Inheritance mode, a class using extends inheritance abstract class, using implement implementation interface
3, abstract class can only single inheritance, interface can be multiple inheritance (interface extends interface) multi-implementation (class Implement Interface);
4, abstract class can have non-abstract methods, interfaces can only have abstract methods, can not have non-abstract methods;
Abstract methods in abstract classes must be decorated with the abstract keyword, which cannot have modifiers in an interface;
5, abstract class is a class, you can have attributes, variables; interfaces can only have constants

(iv) polymorphic

"Polymorphic"
1, a class, is inherited by more than one subclass. If a method of this class shows a different function in multiple subclasses, we call this behavior polymorphic

2. The necessary way to realize polymorphism
① subclass inherits the parent class;
The ② subclass overrides the parent class method;
③ the parent class reference to the child class object;

PHP notes (ii)

Related Article

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.