Last active
December 2, 2021 17:29
-
-
Save Elamurugan-Nallathambi/14371de9770fe1fac7e944d08869b952 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class ProtectedClass | |
| { | |
| public function execute($called) | |
| { | |
| echo 'Called ProtectedClass from ' . $called . PHP_EOL; | |
| } | |
| } | |
| class PrivateClass | |
| { | |
| public function execute($called) | |
| { | |
| echo 'Called PrivateClass from ' . $called . PHP_EOL; | |
| } | |
| } | |
| class ParentClass | |
| { | |
| public $public = 'Public'; | |
| protected $protected = 'Protected'; | |
| protected $protectedObj; | |
| private $private = 'Private'; | |
| private $privateObj; | |
| public function __construct(ProtectedClass $protectedClass, PrivateClass $privateClass) | |
| { | |
| $this->protectedObj = $protectedClass; | |
| $this->privateObj = $privateClass; | |
| } | |
| } | |
| class InheritedClass extends ParentClass | |
| { | |
| // private $private; | |
| // protected $privateObj; | |
| public function __construct(ProtectedClass $protectedClass, PrivateClass $privateClass) | |
| { | |
| parent::__construct($protectedClass, $privateClass); | |
| // $this->private = parent::$private; | |
| // $this->privateObj = $privateClass; | |
| } | |
| function printData() | |
| { | |
| echo $this->public . PHP_EOL; | |
| echo $this->protected . PHP_EOL; | |
| $this->protectedObj->execute(__METHOD__); | |
| echo $this->private . PHP_EOL; | |
| $this->privateObj->execute(__METHOD__); | |
| } | |
| } | |
| $protectedClass = new ProtectedClass(); | |
| $privateClass = new PrivateClass(); | |
| $obj2 = new InheritedClass($protectedClass, $privateClass); | |
| $obj2->printData(); // private references are not accessible | |
| // Requires PHP 7.4 above |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment