Re: conditional classes

From: Date: Sun, 24 May 2009 09:13:27 +0000
Subject: Re: conditional classes
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-293055@lists.php.net to get a copy of this message
kranthi wrote:
thanks for the comments, what i m planning to do is.... function _autoload($class) { if($class == 'Database') { if(class_exis('PDO') {
    include_once('Database_PDO.php');
} else {
    include_once('Database.php');
} } where in Database_PDO.php contains class Database extends PDO { } Database.php contains class Database { }
appears to me it should be: abstract class Database { class PDODatabase extends Database { class MysqlDatabase extends Database { ...etc because if you don't have PDO how can you extend it...? ;) then class Persistence { private static $database; public function select( $sql ) {
     self::getDatabase()->query( $sql );
} public static function getDatabase() {
    if( !(self::$database instanceof Database) ) {
if( class_exists( 'PDODatabase' ) ) {
          self::$database = new PDODatabase();
        } elseif( function_exists('mysql_query') ) {
          self::$database = new MysqlDatabase();
        }
    }
    return self::$database;
} } or suchlike one thing you will need to consider is getting database connection settings in there for instantiation; in which case injecting a configured instance of the required database handler in to the persistence layer may be more suitable.

« previous php.general (#293055) next »