OOP beginner: classB extends classA. classA already object. method in classB needed.. etc.
Posted
by Yvo
on Stack Overflow
See other posts from Stack Overflow
or by Yvo
Published on 2010-03-17T12:55:27Z
Indexed on
2010/03/17
13:01 UTC
Read the original article
Hit count: 121
php
|object-oriented-design
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
© Stack Overflow or respective owner