Important knowledge points in PHP object-oriented (1)

From: http://www.cnblogs.com/stephen-liu74/p/3497440.html

1. __construct: 

     Built-in constructor, called automatically when the 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 is called...\n";
    }
    public function printAttributes() {
        print '$arg1 = '.$this->arg1.' $arg2 = '.$this->arg2."\n";
    }
}
$testObject = new ConstructTest("arg1","arg2");
$testObject->printAttributes();

  

     The results are as follows:

Stephens-Air:Desktop$ php Test.php
__construct is called...
$arg1 = arg1 $arg2 = arg2

2. parent: 

     It is used to directly call the method in the parent class in the subclass, and the function is equivalent to super in Java. 

<?php
class BaseClass {
    protected $arg1;
    protected $arg2;

    function __construct($arg1, $arg2) {
        $this->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";
copy code

     The results are as follows:

Stephens-Air:Desktop$ php Test.php
__construct is called...
$arg1 = arg1 $arg2 = arg2 $arg3 = arg3

3. self:

     The prefix decoration of static members and static methods of the class is called within the class, and this is used for non-static member variables and functions. 

copy code
<?php
class StaticExample {
    static public $arg1 = "Hello, This is static field.\n";
    static public function sayHello() {
        print self::$arg1;
    }
}

print StaticExample::$arg1;
StaticExample::sayHello();
copy code

     The results are 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 newly added in PHP 5.3 and later. Please take a look at the code and critical comments.

copy code
<?php
abstract class Base {
    public static function getInstance() {
        //The new static() here instantiates the current class that calls the static method.
        return new static();
    }
    abstract public function printSelf();
}

class SubA extends Base {
    public function printSelf() {
        print "This is SubA::printSelf.\n";
    }
}

class SubB extends Base {
    public function printSelf() {
        print "This is SubB::printSelf.\n";
    }
}

SubA::getInstance()->printSelf();
SubB::getInstance()->printSelf();
copy code

     The results are as follows:

Stephens-Air:Desktop$ php Test.php
This is SubA::printSelf.
This is SubB::printSelf.

     The static keyword can be used for more than just instantiation. Like self and parent, static can also be used as an identifier for static method calls, even from non-static contexts. In this scenario, self still represents the class where the current method is located. See the following code: 

copy code
<?php
abstract class Base {
    private $ownedGroup;
    public function __construct() {
        //The static here is the same as the above example, indicating the actual class that is currently calling the method.
        //It should be noted that the getGroup method here will get the same result even if it is not a static method. However if
        //getGroup is really just a normal class method, so it is recommended to use $this here.
        $this->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(); 
copy code

     The results are as follows:

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

5. __destruct:

     The role of the destructor method is just the opposite of the constructor method __construct. It is only called automatically before the object is collected by the garbage collector. We can use this method to do some necessary cleanup work.

copy code
<?php
class TestClass {
    function __destruct() {
        print "TestClass destructor is called.\n";
    }
}

$testObj = new TestClass();
unset($testObj);
print "Application will exit.\n";
copy code

     The results are as follows:

Stephens-Air:Desktop$ php Test.php
TestClass destructor is called.
Application will exit.

6. __clone:

     In versions after PHP 5, the assignment between objects is a reference assignment, that is, the two objects after assignment will point to the same address space. If you want to assign values ​​based on objects, you can use the clone method provided by PHP. This method returns the copy of the current object after the shallow copy. If you want to complete some special operations in the process of clone, such as deep copy, you need to implement the __clone method in the declaration of the current class, which is in the process of executing the clone. will be called implicitly. In addition, it should be noted that the __clone method is applied to the object to be copied, that is, the object after the assignment is executed.

copy code
<?php
class InnerClass {
    public $id = 10;
    public function printSelf() {
        print '$id = '.$this->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();
copy code

     The results are as follows:

Stephens-Air:Desktop$ php Test.php
Before calling to clone.
__clone is called.
After calling to clone.
In outerA: $id = 20
In outerB: $id = 10

7. const:

    PHP5 can define constant properties in classes. Like global constants, once defined, they cannot be changed. Constant properties do not need to start with $ like normal properties. By convention, only uppercase letters can be used to name constants. In addition, like static properties, constant properties can only be accessed through the class and not through the instance of the class. When referring to constants, you do not need to use the $ sign as the leading character. In addition, constants can only be assigned to basic types, such as integers, and cannot point to any object type.

<?php
class TestClass {
    const AVAILABLE = 0;
}

print "TestClass::AVAILABLE = ".TestClass::AVAILABLE."\n";

    The results are as follows:

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325262052&siteId=291194637