Common inheritance procedures, and the understanding of public, private, protected modifiers:
/***************************** Parent Class ********************************//** * Person parent class*/classPerson { Public $name;//Common attributes can be accessed inside and outside the class, and can be inherited by the quilt class Public $age; Public $sex; protected $myprotect;//protected Properties, cannot be accessed externally, can only be accessed within a class, and subclasses of this class can access Private $myprivate;//private properties, which can only be accessed internally by this class, cannot be accessed externally, and cannot inherit access from the Quilt class Public Static $MYSTATICOBJ;//global static variables, accessed directly through the class method /** * Construction method*/ function__construct ($name,$age,$sex) { $this->name =$name; $this->age =$age; $this->sex =$sex; $this->myprotect = "Myprotect"; $this->myprivate = "Myprivate"; } /** * Common instance methods, which can be accessed inside and outside the class, and can be overridden by subclasses*/ Public functionPersonInfo () {Echo"Name:".$this->name. " \ n "." Age: ".$this->age. " \ n "." Gender: ".$this->sex. " \ n "." Myprotect: ".$this->myprotect. " \ n "." Myprivate: ".$this-myprivate; } /** * Private instance method, which can be accessed by the inside of the class, but cannot be accessed by the outside of the class, nor the quilt class inherits Access*/ Private functionMySecret () {Echo"This is my secret, I can't tell you."; } /** * Protected instance method, can be accessed by the inside of the class, can also be accessed by the quilt class, but cannot be accessed outside the class*/ protected functionMyprotectfun () {Echo"This is my Myprotectfun method."; } Public Static functionShare () {Echo"Static method, called directly through the class name"; } function__destruct () {Echo"God Damn,i die"; }}/***************************** sub-class ********************************//** * Subclasses that inherit from person*/classStudentextendsperson{ Public $grade; function__construct ($name,$age,$sex,$grade) {Parent:: __construct ($name,$age,$sex); $this->grade =$grade; } Public functionPersonInfo () {Parent::PersonInfo (); Echo"\ n". " Grade: ".$this->grade. " \ n "; /*Result: Name: KLP Age: 24 Sex: Male Myprotect:myprotect myprivate:mypriv Ate Grade:six This is my sub-class Myprotectfun method*/ $this-Myprotectfun (); } protected functionMyprotectfun () {Echo"This is my sub-class Myprotectfun method"; } Public Static functionShare () {Echo"Subclass static method, called directly through the class name"; }}//$obj = new Person ("KLP", "n", "male");//$obj->personinfo ();//$obj->myprotect; Error: Fatal error:cannot Access protected Property Person:: $myprotect//$obj->mysecret (); /* ERROR: Fatal Error:call to Private Method Person::mysecret () from context*///person::share (); /* static method, directly called by the class name * /$stu=NewStudent ("KLP", "$", "male", "six");$stu-personInfo ();$stu->myprivate;/*returns empty, but does not give an error*/Student:: Share ();/*subclass static method, called directly from the class name*/
PHP Object-oriented understanding (i)