1 Using Extends to achieve inheritance and overload, the meaning of the magic method
Class B Extends a
When the declaration is time, there is no method in A.
$ B = new b () when calling
Method () in $ b-> a ();
$ b-> a attribute = 1;
Methods in $ b-> b ();
Methods in $ b-> b ();
If $ a = new a ();
can
Method of $ A-> A ();
Attributes in $ A-> A = 1;
Cannot
Methods in $ A-> B ();
Methods in $ A-> B ();
Overload: b Inherits the method attributes of implementing and the same name in A, B.
"Overload" in PHP is different from the vast majority of object-oriented languages. Traditional "overload" is a class method for providing multiple symbols, but the parameters and numbers of each method are different.
Magic Method: PHP regards all the methods of __ (two underscores) as a magic method. So you define your own class method, don't prefix in __.
2 Inherit with private and protected to access modifier visibility
Attribute method private can't be inherited
Attribute method The protected area is not visible outside, which can be inherited.
The class member defined by the attribute method PUBLIC can be accessed anywhere.
3 PHP in double colon :: application
The "::" operator is often seen in the PHP class code. This is a scope qualifier, which is used to use a double-selling number "::", which is used to set the level of different scope in the top class. The left is a member of the action domain is a member of the access scope.
The scope defined in PHP has two types of Self and Parent (a STATIC action domain is provided in PHP6).
The range parsing operator (also known as Paamayim Nekudotayim) or is more simply as a pair of colons, which can be used to access static members, methods, and constants, and can be used for subclasses to overwrite members and methods in the parent class.
Class myclass {const const_value = 'a constant value';} echo myclass :: const_value;
Class OtherClass Extends Myclass {public static $ my_static = 'static var'; public static function doublecolon () {echo parent :: const_value. "\ n"; echo self: $ my_static. "\ n";}} OtherClass :: Doublecolon ();
// Subclass covers the parent class
Class myclass {protected function myclass :: myfunc () \ n ";}} class OtherClass Extends myclass {// Overwrite method in the parent class public function myfunc () {// But still call has been called Covered Method Parent :: myfunc (); echo "Otherclass :: myfunc () \ n";}} $ class = new otherclass (); $ class-> myfunc ();
4 PHP in THIS and SELF and PARENT
THIS: It is a pointer to the current object instance, does not point to any other object or class.
Self: Indicates the current class's scope, which is different from this is that it does not represent a particular example of the class, and self cannot be used in the code outside the class, and it cannot recognize the location of ourselves in inheritance. That is, when using SELF in the extended class, it is called a method of the parent class, but a method of extending the overload of the class. Self is a class of itself, that is, Self is an object that does not point to any instantiated, general Self use to point to static variables in the class. Private static $ firstcount = 0; private $ lastcount; // Constructor function __construct () {$ this-> lastcount; // use self to call static variables, using the SELF call must be used: :( Domain arithmetic symbol)}
PARENT: Indicates the scope of the current parent class, the rest of the Self feature. Parent is a pointer to the parent class, usually we use Parent to call the constructor of the parent class.
// Inherited constructor function __construct ($ Personsex, $ Personage) {parent :: __ construct ("test"); // Using Parent calls the constructor of the parent class $ this-> Personsex = $ Personsex; $ this- > Personage = $ PERSONAGE;}
5 constructor and destructor
Classes with constructor call this method first at each creating objects, so it is very suitable for some initialization work before using the object.
Function __construct () {} If the constructor defines the constructor in the subclass, the constructor of its parent class will not be invoked. To perform the constructor of the parent class, you need to call Parent :: __ construct () in the constructor of the subclass.
PHP 5 introduces the concept of a destructor, similar to other object-oriented languages, such as C . The destructor will be deleted by all references to an object or execute when the object is explicitly destroyed.
Function __destruct () {}
6 final keyword
PHP 5 added a Final keyword. If the method in the parent class is declared as Final, the subclass cannot overwrite the method; if a class is declared as Final, it cannot be inherited.
7 inheritance and constructor
The result of the parent class subclass with constructor unconfluenzable function parent constructs a constructor having a constructor
8 interface
You can define an interface through Interface, just like defining a standard class.
note:
1) But all methods are empty;
2) All methods defined in the interface must be public, which is the characteristics of the interface;
3) When implementing multiple interfaces, the methods in the interface cannot be renamed;
4) The interface can also be inherited, by using an Extends operator;
5) Constants can also be defined in the interface. The use of interface constants and class constants is exactly the same. They are all set, and they cannot be modified by subclasses or sub-interfaces.
// Declare a 'Itemplate' Interface Interface Itemplate {Public Function SetVariable ($ Name, $ VAR); Public Function GethTML ($ Template);}
// Implement the interface // The following is the correct class template imports itemplate {private $ VARS = array (); public function setvariable ($ name, $ var) {$ this-> VARS [$ name] = $ var;} Public Function gethtml ($ TEMPLATE) {FOREACH ($ THIS-> VARS AS $ Name => $ value) {$ Template = STR_REPLACE ('{'. $ name. '}', $ value, $ template;} return Template;}} 9 Variable members of the property class are called "Properties", and the property declaration is started by the keyword public or protected or private, and then consists of a variable. The variables in the property can be initialized, but the value of the initialization must be a constant, and the constant here refers to a constant when the PHP script is constant during the compilation phase, rather than the constant that operates during the compilation phase.
In PHP5, two functions "__get ()" and "__set ()" and "__set ()" are predefined and assigned the "__ISSET ()" and delete properties "__unset ()".
Simply talk about the value, one is assignment. , "__ set ()" and "__get ()" These two methods are not the default, but we are hand-added to the class, like constructor (__construct (), add It will exist, you can add these two methods as follows, of course, you can also add: // __ get () method to get private properties by personal style.
View Plain
Copy to CLIPBOARD
?
PHP Class Person {// The following is a human member property private $ name; //man's name Private $ SEX; // people 'gender private $ age; // people age // __ get () method is used to get Private property private function __get ($ putrty_name) {if (isset ($ this -> $ factty_name)) {Return ($ this -> $ proty_name);} else {return (null);}}} //__ set () method Used to set private property private function __set ($ putrty_name, $ value) {$ this -> $ proty_name = $ value;} // __ isset () method private function __isset ($ nm) {echo "isset () function measurement private member Automatic call
"; return isset ($ this -> $ nm);} // __ unset () method private function __unset ($ nm) {echo" When using unset () functions externally () function to delete private members Automatic call
; unset ($ this -> $ nm);}}}} $ p1 = new person (); $ p1-> name = "this is a person name"; // Using isset () When the function determines private members, automatically calls the __isset () method to help us complete, return the result for true echo var_dump (isset ($ P1-> Name)). "
"; Echo $ P1-> Name. "
"; // When using the unset () function to delete private members, automatically call the __unset () method to help us complete, remove the Name private property unset ($ p1-> name); // has been deleted, the line There will be no output Echo $ P1-> name;?> Php class person {// below is the human member property Private $ name; // people 's name Private $ sex; // humanity Don't private $ age; /// human age // __ get () method Used to get private property private function __get ($ proty_name) {if (isset ($ this -> $ putrty_name) {Return ($ this -> $ proty_name Else
{RETURN (NULL);}}} // __ set () method is used to set private property private function __set ($ stroperty_name, $ value) {$ this -> $ proty_name = $ value;} // __ isset () method Private function __isset ($ nm) {echo "iSSET () function to measure private member, automatically call
"; Return Isset ($ this -> $ nm);} // __ unset () method private function
__unset ($ nm) {echo "When using the unset () function externally () function to delete the
" "unset ($ this -> $ nm);}}} $ p1 = new person (); $ P1-> name = "this is a person name"; // When using the iSSet () function to measure private member, automatically call __isset () method to help us complete, return the result for TRUE Echo var_dump (isset ($ P1- > name)). "
"; Echo $ P1-> name. "
"; // When using the unset () function to delete private members, automatically call __unset () method to help us complete, delete Name Private properties unset ($ p1-> name); // has been deleted, this line does not have output Echo $ P1-> Name;?>
10 clone
Object replication can be done by clone keyword (if there is a __clone () method in the object, it will be called first). The __clone () method in the object cannot be called directly.
When the object is copied, PHP5 performs all the properties of the object "Shallow Copy. The references in all properties are still constant, pointing to the original variable. If the __clone () method is defined, the __clone () method in the newly created object (copy generated object) will be called, which can be used to modify the value of the attribute (if necessary).