NYCPHP Meetup

NYPHP.org

[nycphp-talk] Accessing a method from one class in another

Randal Rust randalrust at gmail.com
Wed Jan 18 08:58:30 EST 2006


On 1/14/06, Daniel Convissor <danielc at analysisandsolutions.com> wrote:

> Easiest thing to do is in the class_content class, have a property named
> $db and set it in the constructor.

That was actually what I used to do. What I didn't like about it was
that everytime I had to make a call to a method in the $db class, I
was writing....

$this->db->conn->Execute($sql)
$this->db->connErrorMsg()

...and so on.

Instantiating the DB outside of the class, and then passing it as an
argument allowed me to shorten things to...

$db->Execute($sql)
$db->ErrorMsg()

> Or, have all your classes extend from the DB class.

I tried working with that some yesterday, but for some reason I
couldn't quite get it to work.

For the time being, I think I'm OK passing the DB as an argument. No
one has really said that there is anything 'wrong' with that approach.

I guess my options are:

- set a global variable, which has to be passed to each method
- pass it as an argument to each method (pretty much the same as
global, but no chance of it being overwritten)
- instantiate it in the constructor, like I do with my other helper methods
- create a singleton, which I haven't quite figured out yet

--
Randal Rust
R.Squared Communications
www.r2communications.com



More information about the talk mailing list