Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access child property from parent scope

Tags:

oop

php

I need a parent class to access its child properties:

class Parent {
  private $_fields = null;

  public function do_something() {
    // Access $_fields and $child_var here
  }
}

class Child extends Parent {
  private $child_var = 'hello';
}

$child = new Child();
$child->do_something();

When $_fields is modified from the child scope, it's still null in the parent scope. When trying to access $child_var from parent scope using $this->child_var, it's of course undefined.

I didn't find anything like a "function set" that would just be copied in the child class...

like image 345
ldiqual Avatar asked Sep 19 '11 10:09

ldiqual


People also ask

Can parents access child attributes?

Basically, you cannot access parent's private properties/methods nor can the parent access its child's. However, you can declare your property/method protected instead.

How do you access child controller scope in parent controller?

In angular there is a scope variable called $parent (i.e. $scope. $parent). $parent is used to access parent scope from child controller in Angular JS.

Can parent class access child variables?

The reference holding the child class object reference will not be able to access the members (functions or variables) of the child class. This is because the parent reference variable can only access fields that are in the parent class.

What is scope parent?

Angular scopes include a variable called $parent (i.e. $scope. $parent ) that refer to the parent scope of a controller. If a controller is at the root of the application, the parent would be the root scope ( $rootScope ). Child controllers can therefore modify the parent scope since they access to it.


2 Answers

Take a look at an article about visibility.

Basically, you cannot access parent's private properties/methods nor can the parent access its child's. However, you can declare your property/method protected instead.

class Parent {
    protected $_fields = null;

    public function do_something() {
        // Access $_fields and $child_var here
    }
}

class Child extends Parent {
    protected $child_var = 'hello';
}

$child = new Child();
$child->do_something();
like image 107
kgilden Avatar answered Oct 18 '22 01:10

kgilden


Trying to access child values from base(parent) class is a bad design. What if in the future someone will create another class based on your parent class, forget to create that specific property you are trying to access in your parent class?

If you need to do something like that you should create the property in a parent class, and then set it in child:

class Parent 
{
    protected $child_var;

    private $_fields = null;    
    public function do_something() 
    {     
        // Access $_fields and $child_var here
        //access it as $this->child_var   
    }
}
class Child extends Parent 
{   
    $child_var = 'hello'; 
}  

$child = new Child(); 
$child->do_something(); 

Basically in parent you should not reference child-specific content, because you can't be sure it will be there!

If you have to, you should use abstraction:

PHP Abstraction

like image 22
Daniel Gruszczyk Avatar answered Oct 17 '22 23:10

Daniel Gruszczyk