Inheritance and method overloading of PHP classes

Source: Internet
Author: User
To load a class using the Autoloader: (abbreviated version)

Spl_autoload_register (function ($className) {    require './class/'. $className. PHP '; });

$smartPhone = new SmartPhone (' Apple ', ' IPhone8 ', 5888);

At this point the smartphone class does not have these three properties, can output it?

echo ' Brand: '. $smartPhone->brand. ' <br> '; Normal: Public can be accessed externally

echo ' model: '. $smartPhone->model. ' <br> '; Error: Protected is only accessed in the current class and subclass

Echo ' Price: '. $smartPhone->price. ' <br> ';//Error: Private limited to current class access only

* If you can access these properties properly?

* There are two solutions to achieve

* 1. Set all access controls for related properties in parent class mobilephone to public

* 2. Set the access control for the related property in parent class Mobilephone to protected and create a query in subclass smartphone

* We use the second option

 $smartPhone = new SmartPhone (' HUAWEI ', ' P20 ', 5488,true,true);// Let's change a set of data to initialize the object, verify that Parent::__contrunctor () $smartPhone = new SmartPhone (' MI ', ' MIX2 ', 3599,true,true);// At this point the SmartPhone class does not have these three properties, can output it? Echo ' Brand: '. $smartPhone->brand. ' <br> '; echo ' model: '. $smartPhone->model. ' <br> '; Echo ' Price: '. $smartPhone->price. ' <br> ';//Output two properties extended in subclass Echo ' Photography: '. ($smartPhone->camera? ' Support ': ' No '). ' <br> '; Echo ' Internet: '. ($smartPhone->internet? ' Support ': ' No '). ' <br> '; Echo $smartPhone->call (). ' <br> '; Call () is the method in the parent class echo $smartPhone->game (). ' <br> '; Game () is a method in a subclass 
Related Article

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.