03-what does a subclass inherit from the parent class?

Source: Internet
Author: User
<? PHP // study what can be inherited when a class inherits from another class. Class A {// public attribute public $ name = "" of the parent class; // The protected attribute protected $ age = "20" of the parent class "; // Private Property of the parent class private $ sex; // public method of the parent class public function testing1 () {echo "test1 ()";} // protected method of the parent class protected function testing2 () {echo "Test2 ()";} // Private method of the parent class private function testging3 () {echo "test3 ()" ;}} Class B extends a {public function getage () {return $ this-> age;} public function gotest2 () {$ this-> testing2 () ;}$ b1 = new B (); // 1. it proves that the variable modified by public can inherit echo $ B1-> name; // 2. it proves that the variable modified by protected can also inherit echo $ B1-> getage (); // 3. private variables cannot be inherited by the quilt class. // 4. The public function can be inherited from $ B1-> testing1 (); // 5. The protected method can be inherited. $ B1-> gotest2 (); // 6. Private methods cannot be inherited.?>

Key points:


In PHP, inheritance is a single inheritance, that is, one class can only inherit another class,

If you want a class to inherit multiple classes, you can use multiple inheritance methods, that is, Grandpa's variable father inherits, and Dad's variable son inherits,

Therefore, the son inherits the attributes of both Grandpa and dad.

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.