PHP Object-Oriented Programming – Sharing Objects Between Classes

object-orientedPHP

edit: I am thinking that dependency injection is the best approach.

I am struggling to wrap my head around being able to share an object between two classes. I want to be able to create only one instance of the object, commonlib in my main class and then have the classes, foo1 and foo2, to be able to mutually share the properties of the commonlib. commonlib is a 3rd party class which has a property Queries that will be added to in each child class of bar. This is why it is vital that only one instance is created. I create two separate queries in foo1 and foo2.

This is my setup:

abstract class bar{
//common methods
}


class foo1 extends bar{
//add query to commonlib
}

class foo2 extends bar{
//add query to commonlib
}

class main {

    public $commonlib = new commonlib();

    public function start(){
    //goal is to share one instance of $this->commonlib between foo1 and foo2 
    //so that they can both add to the properites of $this->commonlib (global
    //between the two)

    //now execute all of the queries after foo1 and foo2 add their query
    $this->commonlib->RunQueries();

}



}

Best Answer

Approach 1: Use a property in bar

Make commonlib a property in bar and declare getters and setters. Share that one instance of commonlib that you declared in main between foo1 and foo2.

abstract class bar {
    protected $commonlib;
    
    public function __get($property) {
        if (property_exists($this, $property)) {
            return $this->$property;
        }
    }

    public function __set($property, $value) {
        if (property_exists($this, $property)) {
            $this->$property = $value;
        }
        return $this;
    }
}


class foo1 extends bar{
    public function someMethod() {
        $this->commonlib->addQueries($fromSomeWhere);
    }
}

class foo2 extends bar{
    public function someMethod() {
        $this->commonlib->addQueries($fromSomeWhereElseAndInADifferentWay);
    }
}

class main {

    public $commonlib = new commonlib();

    public function start(){
        //goal is to share one instance of $this->commonlib between foo1 and foo2 
        //so that they can both add to the properites of $this->commonlib (global
        //between the two)
        $foo1 = new foo1();
        $foo1->commonlib = $commonlib;

        $foo2 = new foo2();
        $foo2->commonlib = $commonlib;

        $foo1->someMethod();
        $foo2->someMethod();

        //now execute all of the queries after foo1 and foo2 add their query
        $this->commonlib->RunQueries();

}

Edit:

Approach 2: Pass common lib as an argument and only put algos in foo1 and foo2

Thus,

class foo1 {
    public function addQueries($commonlib) {
        $commonlib->addQueries($fromSomeWhere);
    }
}

class foo2 {
    public function addQueries($commonlib) {
        $commonlib->addQueries($fromSomeWhereElseAndInADifferentWay);
    }
}

class main {

    public $commonlib = new commonlib();

    public function start(){
        //goal is to share one instance of $this->commonlib between foo1 and foo2 
        //so that they can both add to the properites of $this->commonlib (global
        //between the two)
        $foo1 = new foo1();
        $foo2 = new foo2();

        $foo1->someMethod($commonlib);
        $foo2->someMethod($commonlib);

        //now execute all of the queries after foo1 and foo2 add their query
        $this->commonlib->RunQueries();

}
Related Topic