I also don't really get the idea of interface and abstract classes ? They don't seem to be any practical use ? Maybe that's me ? Anyway, not really relevant to this post ...
there is a lot of usefulness in these constructs; look into design patterns. also, there are code libraries already written w/ the abstraction you are trying to develop. im not discouraging you from developing your own, but it may be helpful for you to study some of the other code. check out http://pear.php.net/package/MDB2 http://ez.no/doc/components/view/2007.1/(file)/classtrees_Database.html http://www.onphp.org/doxy/trunk/ -nathan ps. i believe interjinn provides a db layer as well :> On 7/11/07, Steve Perkins <sp@xxxxxxxxxxxx> wrote:
OK, so that came out fairly illegible. Try again: Hi, new to PHP5 (and the forums evidently !) and I have a question about the object model. I want to be able to create a class which is allows abstraction from specifics. So for one example, imagine a generic database connection wrapper which can have multiple drivers depending on the database used. Some of the functionality is generic, some is database specific (mysql_ , odbc_). So: Class MySQL_driver { public prop1 ; public connect() { echo "Foo"; } public runquery() { echo "Foo"; } ... } Class ODBC_driver { public prop1 ; public connect() { echo "Foo"; } public runquery() { echo "Foo"; } ... } Class generic { public $driver = null ; function __construct($connection_type) { if $this->connection_type = "MySQL" { $this->driver = new MySQL_Driver ; } else { $this->driver = new ODBC_Driver ; } } // Using the non-generic connect() from the selected driver function authenticate { $this->driver->prop1 = "fooey" // These references work fine from within // the generic class echo $this->driver->prop1 ; echo $this->driver->function1 ; } // Report non-generic errors from the selected driver function errors { } function debug { } ... } $gen = new generic("MySQL") ; // Instantiate the generic database object, which // determines its own driver $gen->driver->prop1 = "fooey" // but they fail from here echo $gen->driver->prop1 ; echo $gen->driver->function1 ; // this works ok though, so everything is created // correctly $gen->authenticate() ; So I want to access generic functions as $generic->authenticate(), and database specific functions $generic->driver->runquery. This allows the top level code to be able to be completely generic and not know anything about the underlying database. But I can't ! I can't find a way of accessing the encapsulated object (driver) from the instantiator of generic. Is it possible ? I guess there are lots of "workaround" ways. I could write lots of "middleman" code in generic and use things like __set and __get etc but its lots of extra overhead. I also know I could use extends but that makes the code the wrong way around (MySQL_Driver would have to extend generic), hence the top-level application would have to include code to determine which driver to create rather than the db object determining its own connection driver to use. This is true also for just lumping everything in a single class per db type. Surely it must be possible ? Or am I missing something ? I also don't really get the idea of interface and abstract classes ? They don't seem to be any practical use ? Maybe that's me ? Anyway, not really relevant to this post ... Please, please can someone help !! Thanks -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php