PHP is oriented to object basics (interfaces and classes) and php is oriented. PHP object-oriented basics (interfaces and classes), php object-oriented basics of PHP object-oriented 1. interface definition interface, class definition class, class support abstract and final modifier, abstract modifier PHP object-oriented basic object (interface, class), php oriented
Introduction to Basic PHP object-oriented knowledge
1. interface definition interface, class definition class, class support abstract and final modifier, abstract modifier as abstract class, abstract class
Direct instantiation is not supported. final-modified classes/methods cannot be inherited or overwritten.
2. the interface is implemented through implements, and the class inherits extends
interface IShape{ function draw_core(); }; class PathShape implements IShape{ public function draw_core(){} } class Rectangle extends PathShape{ public function draw_core(){ //overide draw_core } }
3. static variables and constants (static, const)
A. The constant declaration variable name does not need to be preceded by the $ modifier. static variables need
B. Both use class access. when using static variable methods, you must add the $ USD modifier before the variable name.
class MyClass{ const M_CONST_VALUE; static $M_STATIC_VALUE; } MyClass::M_CONST_VALUE ; MyClass::$M_STATIC_VALUE;
C. when a constant is declared, the access permission modifier cannot be added before the const, and the constant is public by default.
const M_CONST ; //OK public const M_CONST ; // throw exception
4. internal class access non-static/constant variables and methods through $ this, access the parent class through parent, access static variables and methods within the class can be through
Self, which is essentially directed to this class and can also be accessed through static
Parent: method (); // parent class method $ this-> method (); // method instance method self: $ static_value; // access static variable static :: $ static_value; // same as above
5. The difference between static and self is that self refers to the parsing context and the function and the current class, and static refers to the called
A typical example is a singleton.
Abstract class ParentClass {public static function createInstance () {return new static (); // self cannot be used here, because self actually points to parentclass. // If you use self, then an exception will be thrown, prompting that the abstract class cannot be instantiated // and static does not directly point to parentclass, but serves to include class //} ChildClass extends ParentClass {//}
7. use the interceptor in the class. The PHP interceptor has _ get ,__ set ,__ inset ,__ unset ,__ call. here we only focus on geth and set interceptors.
_ Get ($ property) when an undefined property is accessed, this method is called _ set ($ property, $ value) class MyClass {public function _ get ($ property) {echo "Access _ get"; if (property_exists ($ this, $ property) {return $ this-> $ property;} else {return "unknown" ;}} public function _ set ($ property, $ value) {if (! Property_exists ($ this, $ property) {$ this-> Name = $ value; // assign a value to $ Name if the variable does not exist} public $ Name = "visonme" ;}; // access $ obj = new MyClass (); $ obj-> Name; // directly access the variable $ Name $ obj-> Password; // The Password is not defined, access _ get first and output unknown //-for _ set $ obj-> password = 'fz-visonme '; // The password does not exist, then, the value of _ setz is assigned to $ Name with echo $ obj-> Name; // output: fz-visonme.
8. class constructor and Destructor :__ construct, _ destruct, called when the constructor instantiates an object. they are mostly used for member variable initialization and called when the class is destroyed, mostly used for finishing work
class MyClass{ function __construct(){} function __destruct(){}}
9. copying objects generates a new object by using the "value replication" method through clone and clone keywords. the object replication itself still adopts reference replication.
A. simple type assignment
class MyClass{ public $ID;};$a = new MyClass;$a->ID = 199;$b = clone $a; echo $b->ID; // output: 199
B. copy of objects contained
Class Account {public $ RMB;}; class MyClass {public $ ID; public $ AccountObj; public function _ construct ($ c) {$ this-> AccountObj = $ c ;};a = new MyClass (new Account (); $ a-> AccountObj-> RMB = 199; $ B = clone $ a; echo $ B-> AccountObj-> RMB; // output: 199 $ a-> AccountObj-> RMB = 100; echo $ B-> AccountObj-> RMB; // output: 100 after cloning, when the AccountObj of $ a changes, it will also affect $ B
This result is obviously not what we expected. what we expect is that AB is two independent objects that do not have any associations.
To solve this problem, we can implement _ clone inside the class. when we call clone outside, the _ clonef method of the class will be called internally, therefore, we can control clone by rewriting _ clone. for example, the transformation of Example B
Class MyClass {public $ ID; public $ AccountObj; public function _ construct ($ c) {$ this-> AccountObj = $ c ;} // _ clone implements clone control. // Here, Account is also cloned. in this case, public function _ clone () is avoided () {$ this-> ID = 0; // Set the ID to 0. if you need it, $ this-> AccountObj = clone $ this-> AccountObj ;}};
We need to know about the _ clone method. this method is called on the cloned object instead of running on the original object. for example, in the example of B
$ B = clone $ a; // execution process: basic copy object $ a ---> $ B execute _ clone ()
Http://www.bkjia.com/PHPjc/1126840.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1126840.htmlTechArticlePHP Object-oriented BASIC (interface, class), php Object-Oriented Introduction to PHP object-oriented basic knowledge 1. interface, class definition class, class support abstract and final modifier, abstract modifier...