Guest User

Untitled

a guest
Nov 20th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. class Parent
  2. {
  3. protected $_property;
  4. protected $_anotherP;
  5.  
  6. public function __construct($var)
  7. {
  8. $this->_property = $var;
  9. $this->someMethod(); #Sets $_anotherP
  10. }
  11.  
  12. protected function someMethod()
  13. ...
  14. }
  15.  
  16. class Child extends Parent
  17. {
  18. protected $parent;
  19.  
  20. public function __construct($parent)
  21. {
  22. $this->parent = $parent;
  23. }
  24.  
  25. private function myMethod()
  26. {
  27. return $this->parent->_anotherP; #Note this line
  28. }
  29. }
  30.  
  31. class MyParent {
  32. protected $data;
  33. public function __construct() {
  34. $this->someMethodInTheParentClass();
  35. }
  36. protected function someMethodInTheParentClass() {
  37. $this->data = 123456;
  38. }
  39. }
  40.  
  41. class Child extends MyParent {
  42. public function __construct() {
  43. parent::__construct();
  44. }
  45. public function getData() {
  46. return $this->data; // will return the $data property
  47. // that's defined in the MyParent class
  48. }
  49. }
  50.  
  51. $a = new Child();
  52. var_dump($a->getData());
  53.  
  54. int 123456
  55.  
  56. class child {
  57.  
  58. public parentObject;
  59.  
  60. public function __construct($parentObject) {
  61. $this->parentObject = $parentObject;
  62. }
  63.  
  64. }
  65.  
  66. public function print_name() {
  67. echo $this->parentObject->name;
  68. }
  69.  
  70. class Child extends Parent
  71. {
  72. private function myMethod()
  73. {
  74. return $this->_anotherP;
  75. }
  76. }
Add Comment
Please, Sign In to add comment