One of the basic concepts of PHP Object-Oriented Learning notes
Source: Internet
Author: User
One of the basic concepts of PHP Object-Oriented Learning notes, if you want to learn PHP object-oriented programming, refer to 1> if ("false") is equivalent to if (true ), because the non-null string is true
2> check the data type:
Is_array ();
Is_object ();
Is_string ();
Is_null ();
Is_integer ();
3> type hint (type hint) of the PHP5 introduced class, used to constrain the parameter type of a method (not the basic data type, but the class ): place the class name before the method parameter to be constrained.
Example: function write (ShopProduct $ shopProduct ){}
4> instanceof operator: if the object in the left operand is of the type shown in the right operand, the result is true.
Example: if ($ shopProduct instanceof BookProduct ){}
5> inherit class son extends parent {}
Methods of the parent class to be called, such as constructor, using parent :__ construct ();
6> static methods and attributes
Class StaticExample {
Static public $;
Static public function hello (){}
}
External access ::
Example: print StaticExample: $;
Internal access using self ::
Example: self: $;
7> abstract class, abstract method
Abstract class xxx {
...
Abstract function write (); // No {}
}
The subclass of the abstract class must re-declare and implement the method. the access control of the newly implemented method cannot be stricter than that of the abstract method.
8> interface
Define only the function, not the implementation. the interface can contain attributes and method declarations, but the method body is empty;
Example: interface {
Public function B ();
}
All classes that implement interfaces must implement all methods defined in interfaces. Otherwise, they must be abstract classes.
Class uses implements in the declaration to implement an interface.
Class Shop implements {
Public function B (){
...
}
}
9> exception
PHP5 introduces exception classes
10> interceptor
_ Get ($ property); called to access undefined properties
_ Set ($ property, $ value); called when assigning values to undefined properties
_ Isset ($ property); called when isset () is used for undefined attributes;
_ Unset ($ property); called When unset () is called for unset attributes;
_ Call ($ method, $ arg_array); called when an undefined method is called
Example: implementation of _ get ()
Copy codeThe code is as follows:
Function _ get ($ property ){
$ Method = "get {$ property }";
If (method_exists ($ this, $ method )){
Return $ this-> $ method ();
}
}
Function _ set ($ property, $ value ){
$ Method = "set {$ property }";
If (method_exists ($ this, $ method )){
Return $ this-> $ method ($ value );
}
}
11> destructor _ destruct ()
12> _ clone (); differences from clone keywords
Class CopyMe ();
$ First = new CopyMe ();
$ Second = $ first;
// PHP4: $ first and $ second are two completely different objects;
// PHP5: $ first and $ second point to the same object
In PHP5, the assignment and transfer of objects are references.
To copy a file, use $ second = clone $ first; // Now $ first and $ second are two completely different objects (by_value copy)
To control replication, you must implement a special method _ clone ()
13> automatic loading: _ autoload ()
PHP5 introduces the _ autoload () interceptor method to automatically include class files. when PHP tries to instantiate an unknown class, it will try to call the _ autoload () method and pass the class name to it as a string parameter.
For example, a simple automatic positioning and inclusion policy:
Function _ autoload ($ classname ){
Includ_once "$ classname. php ";
}
================================
14> use a string to dynamically reference a class
Copy codeThe code is as follows:
$ Classname = "Task ";
Require_once ("tasks/{$ classname}. php );
$ MyObj = new $ classname ();
$ Method = "getTitle ";
$ MyObj-> $ method (); // dynamic method
15> class functions and object functions
Copy codeThe code is as follows:
Class_exist (); // check whether the class exists
Get_declared_classes (); // Obtain all classes defined in the current script process (returned in array format)
Get_class_methods (); // list of all public methods in the class (array)
Method_exist ($ objname, $ method); // whether the object or class method exists
Is_callable (); // object or class methods not only exist, but also can access
Get_class_vars (); // attribute
Get_parent_class (class or object name); // parent class
Is_subclass_of (); // whether it is a subclass. regardless of the interface, the instanceof operator is used for the interface.
16> Reflection API
It consists of a series of built-in classes that can analyze attributes, methods, classes, and parameters, and can dynamically obtain information and call methods.
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