views:

1839

answers:

2

Hello There

http://stackoverflow.com/questions/742097/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?

+3  A: 

The below snippet might be a solution, although I don't really like the circular reference. Try it and use it as you see fit. And by the way, what you are calling class and subclass are actually containing and contained class.

class Database
{
    public $chesterx;

    public function __construct($chesterx)
    {
        $this->chesterx = $chesterx;
    }
}

class Sql
{
    public $chesterx;

    public function __construct($chesterx)
    {
        $this->chesterx = $chesterx;
    }
}

class Chesterx
{
    public $db;

    public $sql;

    public function __construct()
    {
        $this->db  = new Database($this);
        $this->sql = new Sql($this);
    }
}
Ionuț G. Stan
+2  A: 

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

XiroX