public $name; //Attributes of the base class, name $name
//Constructor of the base class, initialization assignment
public function __construct( $name )
{
using using using - ’ ’ s ’ s ’ s ’ ’ s use to ward through so so so so so so so so so so so out out out out out out out out out out out out out out out out out of and out. $personSex; //For derived classes, newly defined attributes $personSex gender, $personAge age
public $personAge;
//Constructor of derived classes
function __construct( $personSex, $personAge)
{
parent::__construct( "PBPHome"); // Use parent to call the constructor statement of the parent class ①
$this->personSex = $personSex;
$this-> personAge = $personAge;
}
//Member function of the derived class, used for printing, format: name is name, age is age
function printPerson()
{
print( $ this->name. " is ".$this->personSex. ",age is ".$this->personAge );
}
}
//Instantiate the Person object
$personObject = new Person( "male", "21");
//Perform printing
$personObject->printPerson();//Output result: PBPHome is male,age is 21
?>
It also contains the usage of this, please analyze it yourself. We pay attention to this detail: the member attributes are all public (public attributes and methods, accessible to code inside and outside the class), especially those of the parent class. This is for inherited classes to access through this. The key point is statement ①: parent::__construct("heiyeluren"). At this time, we use parent to call the constructor of the parent class to initialize the parent class. In this way, the objects of the inherited class are assigned the name PBPHome. . We can test it by instantiating another object $personObject1. After printing, the name is still PBPHome.
Summary: this is a pointer to an object instance, which is determined when instantiating; self is a reference to the class itself, generally used to point to static variables in the class; parent is a reference to the parent class, Generally, parent is used to call the constructor of the parent class.
http://www.bkjia.com/PHPjc/327492.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327492.htmlTechArticle{一}One of the differences between this, self, parent in PHP this article Object Oriented Programming (OOP, Object Oriented Programming ) has now become a basic skill for programmers. Using OOP ideas for advanced PHP...