RE: PHP5 objects access/instantiation model (correction)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Yeah. I've been working on re-writing it using bits from everyone !! It's a
good learning process to find out how PHP handles OOP. I thoroughly
recommend it, it's the only way to learn. Some things just can't be learnt
from the manual!

Cheers All.

-----Original Message-----
From: Richard Lynch [mailto:ceo@xxxxxxxxx] 
Sent: 11 July 2007 21:29
To: Steve Perkins
Cc: php-general@xxxxxxxxxxxxx
Subject: Re:  PHP5 objects access/instantiation model (correction)

You probably ought to have:

class mysql_driver extends generic_driver { }

Seems like that would be the most reasonable OOP model, imho.



That said, I suspect that if you do something like:

$driver = $this->driver;

You then may be able to access the driver-specific data using
$driver->propl; etc.

On Wed, July 11, 2007 10:46 am, Steve Perkins 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
>
>


--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

--
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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux