tags:

views:

71

answers:

4

Hey guys,

I'm learning myself to go from function based PHP coding to OOP. And this is the situation:

ClassA holds many basic tool methods (functions). it's __construct makes a DB connection. ClassB holds specific methods based on a certain activity (extract widgets). ClassB extends ClassA because it uses some of the basic tools in there e.g. a database call.

In a php file I create a $a_class = new ClassA object (thus a new DB connection).

Now I need a method in ClassB. I do $b_class = new ClassB; and call a method, which uses a method from it's parent:: ClassA.

In this example, i'm having ClassA 'used' twice. Onces as object, and onces via a parent:: call, so ClassA creates another DB connection (or not?).

So what is the best setup for this basic classes parent, child (extend) situation? I only want to make one connection of course?

I don't like to forward the object to ClassB like this $b_class = new ClassB($a_object); or is that the best way?

Thanks for thinking with me, and helping :d

A: 

You could have another object that holds the Db connection that both class A and B can hook to. And its that class responsability to only make a single connection. You could look into the singleton patter for this

solomongaby
+1  A: 

It doesn't look like you need the instance of class A at all. Make sure that class B really is a class A before using inheritance like this. Look into composition if you aren't sure.

SP
+1  A: 

From what I gather in your description, it sounds like class B should not really inherit from class A. Ask yourself - is B really an "enhanced version" of A? If not, and B is just using some utility from A, it should only call methods on it, or perhaps compose it (contain it as a member variable.)

As an aside, I don't think you should connect to the database in the constructor of A, unless you put some kind of protection around it to prevent duplicate connections. Probably better to have Connect() and Disconnect() functions so it's well understood when you are and are not connected.

Tesserex
B is indeed not a enhanced version of A. I use extend because of the easy parent::method() access. What would be the better way to call a method in classA from classB without having to create a new ClassA object 'everytime', assuming that will cause a DB connection from the __constructor.Could a good solution be in the __construct of classB setting a reference to class A, instead of parent:: accessed via $this->classA?You are right that __construct should not have a db connection, but it makes my case better :s.
Yvo
Yes, when B is constructed it can create a new A and hold onto it in $this->myA. That's what is meant by composition. Of course the situation gets a lot more complicated if multiple objects need access to that same db connection. You don't want everyone creating their own A and making multiple connections. That's why Felix suggested a static connection. But it is also true that overuse of static variables can cause exactly the kind of mess that you're trying to escape.
Tesserex
A: 
$class_a = new ClassA();
$class_b = new ClassB($class_a);

class ClassB {
    private $class_a;

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

If I did understand correctly what you wanted to do. This way you can ClassA methods in every method of ClassB without duplicating the connection to the database.

mhitza
You do understand it correctly! And this is what I also used for a while, but I think it is not very pretty for such a standard issue! Since to access class_b methods, a class_a object needs to be created hence, it's not really a 'blockbox' what I prefer. But if it is standard praktice I can accept.
Yvo
You could instantiate ClassA inside ClassB, or if more classes need access to ClassA than you could employ the Singleton/Factory pattern as @solomongaby pointed above.
mhitza