Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Parent
- {
- protected $_property;
- protected $_anotherP;
- public function __construct($var)
- {
- $this->_property = $var;
- $this->someMethod(); #Sets $_anotherP
- }
- protected function someMethod()
- ...
- }
- class Child extends Parent
- {
- protected $parent;
- public function __construct($parent)
- {
- $this->parent = $parent;
- }
- private function myMethod()
- {
- return $this->parent->_anotherP; #Note this line
- }
- }
- class MyParent {
- protected $data;
- public function __construct() {
- $this->someMethodInTheParentClass();
- }
- protected function someMethodInTheParentClass() {
- $this->data = 123456;
- }
- }
- class Child extends MyParent {
- public function __construct() {
- parent::__construct();
- }
- public function getData() {
- return $this->data; // will return the $data property
- // that's defined in the MyParent class
- }
- }
- $a = new Child();
- var_dump($a->getData());
- int 123456
- class child {
- public parentObject;
- public function __construct($parentObject) {
- $this->parentObject = $parentObject;
- }
- }
- public function print_name() {
- echo $this->parentObject->name;
- }
- class Child extends Parent
- {
- private function myMethod()
- {
- return $this->_anotherP;
- }
- }
Add Comment
Please, Sign In to add comment