Search code examples
phpoopnon-english

Calling Parent Class From Sub Class PHP


$this->a->b->c->d calling methods from a superclass in php

Ive asked a question on this link I ve problem with this tecnique I am able to call the sub classes from a class

like this

$chesterx->db->query();

I wanna do get another class from sub class

for example

i want to query execute which was come from the sql class

            ROOT
             |
sql <---  chesterx --->  db

i wanna use the sql class from db

the problem i cant return the chesterx class from db class

/edit/

I have some classes like news, members, categories, db and query

and i did it like the link which was on the subject top

public function __construct(){

    function __construct(){  
      if(!$this->db){                                       
                 include(ROOT."/func/class/bp.db.class.php");
                 $this->db = new db;
            }
if(!$this->chester){                                        
                 include(ROOT."/func/class/bp.chester.class.php");
                 $this->db = new chester;
            }
        }

i called the db class with this code and now i am able to call and use the db class methods well

for example

i want to use a method from db

that method is containing a value which was returning a data from the chester class's method

i wish i were clarify myself /edit/

is there anyway to do this?


Solution

  • I find Ionut G. Stan's solution good for your case, but you might also want to consider the factory/singleton pattern, though it's only good if your chesterx class is a global one, and only called once