: This article mainly introduces the php object-oriented key points [go]. For more information about PHP tutorials, see.
arg1 = $arg1; $this->arg2 = $arg2; print"__construct is called...\n"; } publicfunctionprintAttributes() {print'$arg1 = '.$this->arg1.' $arg2 = '.$this->arg2."\n"; }}$testObject = new ConstructTest("arg1","arg2"); $testObject->printAttributes();
The running result is as follows:
Stephens-Air:Desktop$ php Test.php __construct is called...$arg1 = arg1 $arg2 = arg2
Used to directly call methods in the parent class in subclass
arg1 = $arg1; $this->arg2 = $arg2; print"__construct is called...\n"; } functiongetAttributes() {return'$arg1 = '.$this->arg1.' $arg2 = '.$this->arg2; }}classSubClassextendsBaseClass {protected$arg3; function__construct($baseArg1, $baseArg2, $subArg3) {parent::__construct($baseArg1, $baseArg2); $this->arg3 = $subArg3; } functiongetAttributes() {returnparent::getAttributes().' $arg3 = '.$this->arg3; }}$testObject = new SubClass("arg1","arg2","arg3"); print$testObject->getAttributes()."\n";
The running result is as follows:
Stephens-Air:Desktop$ php Test.php __construct is called...$arg1 = arg1 $arg2 = arg2 $arg3 = arg3
- Self
Call the prefix modifier of the static member and static method in the class, and use this for non-static member variables and functions.
Static
The static keyword introduced here is mainly used for the delayed static binding function added by PHP 5.3 and later versions.
PrintSelf (); SubB: getInstance ()-> printSelf ();
The running result is as follows:
Stephens-Air:Desktop$ php Test.php This is SubA::printSelf.This is SubB::printSelf.
Static keywords are not only used for instantiation. Like self and parent, static can also be used as the identifier of a static method call, or even called from a non-static context. In this scenario, self still indicates the class of the current method. See the following code:
OwnedGroup = static: getGroup ();} publicfunctionprintGroup () {print "My Group is ". $ this-> ownedGroup. "\ n";} publicstaticfunctiongetInstance () {returnnewstatic ();} publicstaticfunctiongetGroup () {return "default" ;}} classSubAextendsBase {} classSubBextendsBase {response () {return "SubB" ;}} SubA: getInstance ()-> printGroup (); SubB: getInstance ()-> printGroup ();
The running result is as follows:
Stephens-Air:Desktop$ php Test.php My GroupisdefaultMy Groupis SubB
_ Destruct
The role of the constructor is the opposite of the construct _ construct method, which is automatically called before the object is collected by the garbage collector. we can use this method to do some necessary cleaning work.
The running result is as follows:
Stephens-Air:Desktop$ php Test.php TestClass destructoriscalled.Applicationwillexit.
_ Clone
In versions later than PHP 5, the assignment between objects is a reference assignment, that is, the two objects after the assignment point to the same address space. if you want to assign a value based on an object, you can use the clone method provided by PHP. This method returns the copy of the current object after the shortest Copy. if you want to complete some special operations in the clone process, such as the deep copy, you need to implement the _ clone method in the Declaration of the current class. this method will be implicitly called during clone execution. In addition, note that the __clone method applies to the object to be copied, that is, the object to be assigned a value is executed.
id."\n"; }}classOuterClass {public$innerClass; publicfunction__construct() {$this->innerClass = new InnerClass(); } publicfunction__clone() {$this->innerClass = clone$this->innerClass; print"__clone is called.\n"; }}$outerA = new OuterClass();print"Before calling to clone.\n";$outerB = clone$outerA;print"After calling to clone.\n";$outerA->innerClass->id = 20;print"In outerA: ";$outerA->innerClass->printSelf();print"In outerB: ";$outerB->innerClass->printSelf();
The running result is as follows:
Stephens-Air:Desktop$ php Test.php Before calling to clone.__clone is called.After calling to clone.In outerA: $id=20In outerB: $id=10
Const
PHP5 can define constant attributes in a class. Like a global constant, once defined, it cannot be changed. Constant attributes do not need It must start with an uppercase letter. In addition, like static attributes, constant attributes can be accessed only through a class but not through an instance of the class. when a constant is referenced
The above introduces the php object-oriented key points [to], including the content, hope to be helpful to friends who are interested in PHP tutorials.