Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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();

}



}
share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

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();

}
share|improve this answer
    
Just to be clear, you are recommending that I pass commonlib into foo1 construct and then saving it as a property inside of bar? –  Justin Jun 9 '12 at 23:13
    
@Justin yes (in essence). Sorry, I am on a mobile device and cannot post code. Will post it shortly when I get to a PC. –  Monster Truck Jun 9 '12 at 23:23
    
OK...looking forward. –  Justin Jun 9 '12 at 23:25
    
@Justin Added two example code stubs that you can re-use. I don't know PHP so there may be a few syntax errors here and there, hope you will recognize them and avoid them. –  Monster Truck Jun 10 '12 at 0:59
    
Wow, thank! Your examples are crystal clear. I think I am going to go with your first approach. –  Justin Jun 10 '12 at 3:05
show 1 more comment

Isn't Singleton the pattern you are looking for?

The singleton pattern allows you to have only one instance of an object. This instance can generally be called by every object or functions who need it and as it is a single instance every changes will be accessible everywhere.

share|improve this answer
    
Yes, however it is a 3rd party class and I was debating whether or not to create a singleton wrapper out of it or not....I would rather leave the class as it is. –  Justin Jun 9 '12 at 23:26
    
Can't you just use inheritance from commonlib and use this new class as a Singleton? –  lvictorino Jun 9 '12 at 23:31
3  
It will solve the problem but I won't recommend it though. You should use the singleton pattern only when the design of the object itself mandates that it be singleton. –  Monster Truck Jun 9 '12 at 23:31
    
I don't know if reference mechanism exists in PHP but it would solve everything as you would just pass your commonlib as reference to your foo1 and foo2 object and both would link to the same commonlib object. –  lvictorino Jun 9 '12 at 23:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.