Important PHP object-oriented knowledge points (1)

Source: Internet
Author: User
1. _ construct: built-in constructor that is automatically called when an object is created. See the following code :? PhpclassConstructTest {private $ arg1; private $ arg2; publicfunction _ construct ($ arg1, $ arg2) {$ this-arg1 $ arg1; $ this-arg2 $ arg2; print _ construct

1. _ construct: built-in constructor that is automatically called when an object is created. See the following code :? Php class ConstructTest {private $ arg1; private $ arg2; public function _ construct ($ arg1, $ arg2) {$ this-arg1 = $ arg1; $ this-arg2 = $ arg2; print "_ construct

1. _ construct:

Built-in constructor, inObjectIt is automatically called when it is created. See the following code:

 arg1 = $arg1;        $this->arg2 = $arg2;        print "__construct is called...\n";    }    public function printAttributes() {        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

2. parent:

It is used to directly call methods in the parent class in subclass. The function is equivalent to super in Java.

 arg1 = $arg1;        $this->arg2 = $arg2;        print "__construct is called...\n";    }    function getAttributes() {        return '$arg1 = '.$this->arg1.' $arg2 = '.$this->arg2;    }}class SubClass extends BaseClass {    protected $arg3;    function __construct($baseArg1, $baseArg2, $subArg3) {        parent::__construct($baseArg1, $baseArg2);        $this->arg3 = $subArg3;    }    function getAttributes() {        return parent::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

3. 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.

 

The running result is as follows:

Stephens-Air:Desktop$ php Test.php Hello, This is static field.Hello, This is static field.

4. static:

The static keyword introduced here is mainly used for the delayed static binding function added by PHP 5.3 and later versions. Take a look at the code and key comments.

  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 ();} public function printGroup () {print "My Group is ". $ this-> ownedGroup. "\ n" ;}public static function getInstance () {return new static () ;}public static function getGroup () {return "default ";}} class SubA extends Base {} class SubB extends Base {public static function getGroup () {return "SubB" ;}} SubA: getInstance ()-> printGroup (); SubB :: getInstance ()-> printGroup ();

The running result is as follows:

Stephens-Air:Desktop$ php Test.php My Group is defaultMy Group is SubB

5. _ destruct:

The role of the constructor is the opposite of the construct _ construct method.ObjectIt is automatically called before it 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 destructor is called.Application will exit.

6. _ clone:

In Versions later than PHP 5,ObjectBetween values is a reference value, that is, the twoObjectWill point to the same address space, if you wantObjectAssign values. You can use the clone method provided by PHP. This method changes the currentObjectIf you want to complete some special operations in the clone process, such as deep copy, you need to implement the _ clone method in the declaration of the current class, this method is implicitly called during clone execution. In addition, it should be noted that the __clone method is used for copying again.Object, That is,Object.

   id."\n";    }}class OuterClass {    public $innerClass;    public function __construct() {        $this->innerClass = new InnerClass();    }    public function __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

7. const:

PHP5 can define constant attributes in a class. Like a global constant, once defined, it cannot be changed. Constant attributes do not need to start with $ as common attributes. By convention, constants can only be named with uppercase letters. In addition, like static attributes, constant attributes can be accessed only through the class but not through the instance of the class. When a constant is referenced, the $ symbol is not required as the leading character. In addition, a constant can only be assigned as a base type, such as an integer, but cannot point to anyObjectType.

   

The running result is as follows:

0Stephens-Air:Desktop$ php Test.php TestClass::AVAILABLE = 0

Note:KnowledgePoint: Some PHP and otherOrientedObjectThe language is quite special, or it does need to be written for future reference for me.KnowledgePoint. I hope to share it with you.

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.