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