Re: PHP5 objects access/instantiation model (correction)

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

 



some ideas about class design:

1. properties should normally (read almost always) be private.
2. the apps interface to the DB [connection] is via the 'generic'
class, the app should have to know nothing about the drive object and
should have no direct access to it.
3. use an interface definition for your drivers (so they all share a
common set of method ... a common interface ;-))

this leaves you with the problem of wanting to write as little boilerplate
code for making the functionality each driver class implements available via
the 'generic' class.

I would suggest either a few simple functions like so:

class DB {
	function query() {
		$args = func_get_args();
		return call_user_func_array(array($this->driver, __FUNCTION__), $args);
	}
}

which you can simplify into something like:

class DB {
	function __call($meth, $args) {
		$func = array($this->driver, $meth)
		if (!is_callable($func))
			throw new Exception('undefined interface: '.__CLASS__.'::'$meth);

		return call_user_func_array($func, $args);
	}
}

just an idea :-) ... this seems to me to be something along the decorator pattern,
you could choose to do something along the lines of the factory pattern, where by
your factory returns a 'driver' object which your application uses (not caring what
class of driver it is) ... this would also require that your driver classes share a
common interface (actually using a interface definition to enforce it is a seperate issue).

an interface example:

interface DBDriver {
	function query($sql, $args);
	function commit($id);
	function rollback($id);
}

class MySQL_DBDriver implements DBDriver {
	function query($sql, $args) 	{ return mysql_query($sql, $args); }
	function commit($id);		{ return true; }
	function rollback($id);		{ return true; }
}


Steve Perkins wrote:

> 
> // but they fail from here
> 
> echo $gen->driver->prop1 ;                     
> echo $gen->driver->function1 ;

what fails exactly? what is the error?

> 
> // this works ok though, so everything is created
> // correctly
> 
> $gen->authenticate() ;                                 
> 

PS. I would hazard a guess and say the generic class is not very generic -
it's quite specifically a 'database' class, it is an 'abstract' class (not in the
php syntax sense) in that the DBMS specifics are hidden away in helper/plugin classes.

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