note 69348 added to ref.classobj
| From: | kimdothermanssonatgmaildotcom at osu1 dot php dot net | Date: | Fri, 01 Sep 2006 15:40:15 +0000 |
| Subject: | note 69348 added to ref.classobj | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-116588@lists.php.net to get a copy of this message | ||
Note to the solution by 'zabmilenko at hotmail dot com':
Good idea but encapsulation is a better technique which is also more portable and reliable.
You can pass an object which is based on an interface/superclass to the common DB interface.
For instance:
<?php
class DB_Common {
// data can be either an uri or array depending on how you construct it
// e.g. data = array('host' => 'x.x.x.x', 'port' =>
'65535', 'etc' => '...')
function connect($data) { die("must be overriden by subclass"); }
function disconnect() { die("..."); }
function query($query) { die("..."); }
}
class DB_MySQL extends DB_Common { /* override methods here */ }
class DB_PgSQL extends DB_Common { /* same here */
class DB_Frontend {
var $errmsg = array('DB_Common subclass-string or object required');
var $_db;
// PHP4/5 compat
function DB_Frontend($arg) { $this->__construct($arg); }
function __construct($arg) {
if( is_object($arg) && is_a($arg, 'DB_Common') )
$this->_db = $arg;
else
{
if( !is_string($arg) )
die($this->errmsg[0]);
$arg = "DB_$arg";
if( !class_exists($arg) )
die($this->errmsg[0]);
$this->_db = new $arg;
// use is_subclass_of(object, string) for compat with older versions
if( !is_subclass_of($arg, 'DB_Common') )
die($this->errmsg[0]);
}
}
// add connect, disconnect that simply calls $this->_db->method() instead
function getOne($quey) {
// preprocess query if needed
$result = $this->_db->query($query);
// process result if needed
return $result;
}
/* add more methods if needed */
}
// string parameter
$frontend = new DB_Frontend('MySQL');
// object parameter
$backend = new DB_MySQL();
$frontend = new DB_Frontend($backend);
?>
You can also use a static DB class. If you see PEAR::DB you will see how all this is quite equal to
eachother. The main difference is the frontend class which should encapsulte the backend.
Suggestions and/or comments appreciated.
----
Server IP: 129.16.214.50
Probable Submitter: 85.226.145.20
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/ref.classobj.php
Edit -- https://master.php.net/note/edit/69348
Del: integrated -- https://master.php.net/note/delete/69348/integrated
Del: useless -- https://master.php.net/note/delete/69348/useless
Del: bad code -- https://master.php.net/note/delete/69348/bad+code
Del: spam -- https://master.php.net/note/delete/69348/spam
Del: non-english -- https://master.php.net/note/delete/69348/non-english
Del: in docs -- https://master.php.net/note/delete/69348/in+docs
Del: other reasons-- https://master.php.net/note/delete/69348
Reject -- https://master.php.net/note/reject/69348
Search -- https://master.php.net/manage/user-notes.php