Problems referencing derived variables

This is a discussion on Problems referencing derived variables within the PHP General forums, part of the PHP Programming Forums category; Is it possible to reference a variable that is instantiated within the parent class: class Db { public connectionId; public function ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-22-2007
selaz
 
Posts: n/a
Default Problems referencing derived variables

Is it possible to reference a variable that is instantiated within the
parent class:

class Db {
public connectionId;
public function connect($connectString) { $this->connectionId =
databaseDriverConnect($connectString); }
public function query($queryString) { . . . };
public function fetch() { . . . };
.. . .
}

class TableGenerator extends Db {
??? reference to the one and only connectionId ???
/*
- make the query needed to draw a table
- draw the table
*/
.. . .
}

After connecting to the database I want to be able to share the
connectionId inside the TableGenerator class or some other derived
class:

$db = new Db();
$db->connect("SomeConnectString");

$tg = new TableGenerator();
$tg->... draw some table without connecting again ...

Until now I had to make the $db instance global and use it for making
queries inside non derived/nonderived classes, now I want to make it
more OO. I'd like to point out that I need only one connection per
request and several instances like for example TableGenerator,
GraphGenerator,... Is this approach possible and is it the right one??

Thanks.

Reply With Quote
  #2 (permalink)  
Old 03-23-2007
petersprc
 
Posts: n/a
Default Re: Problems referencing derived variables

Using a single instance for the database makes sense, since it will
likely be accessed by many different components and objects. This is
usually known as the singleton pattern.

This can be done as a global, or something like a static method that
returns the instance.

On Mar 22, 7:57 am, "selaz" <ales.z...@gmail.com> wrote:
> Is it possible to reference a variable that is instantiated within the
> parent class:
>
> class Db {
> public connectionId;
> public function connect($connectString) { $this->connectionId =
> databaseDriverConnect($connectString); }
> public function query($queryString) { . . . };
> public function fetch() { . . . };
> . . .
>
> }
>
> class TableGenerator extends Db {
> ??? reference to the one and only connectionId ???
> /*
> - make the query needed to draw a table
> - draw the table
> */
> . . .
>
> }
>
> After connecting to the database I want to be able to share the
> connectionId inside the TableGenerator class or some other derived
> class:
>
> $db = new Db();
> $db->connect("SomeConnectString");
>
> $tg = new TableGenerator();
> $tg->... draw some table without connecting again ...
>
> Until now I had to make the $db instance global and use it for making
> queries inside non derived/nonderived classes, now I want to make it
> more OO. I'd like to point out that I need only one connection per
> request and several instances like for example TableGenerator,
> GraphGenerator,... Is this approach possible and is it the right one??
>
> Thanks.



Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 12:38 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0