PHP Object-oriented understanding (i)

Source: Internet
Author: User

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)

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.