Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To make your example work, you might try this.</p> <pre><code>define('DB_CONN','mysql:dbname=;host='); define('DB_USER', ''); define('DB_PASS', ''); interface iMySQL { public function query( $string ); public function select(); public function selectAll(); public function insert(); public function update(); public function delete(); public function load(); } class DataBase { /** * @var object PDO */ private static $instance = null; /** * Cria uma instância do PDO representando a conexão ao banco de dados e torna a instância disponível como "singleton" * * @param string $dsn O DSN completo, ex.: mysql:host=localhost;dbname=testdb * @param string $username O nome de usuário para a string DSN. Esse parâmetro é opcional para alguns drivers do PDO. * @param string $password A senha para a string DSN. Esse parâmetro é opcional para alguns drivers do PDO. * @param array $driver_options Um array key =&gt; value de opções de conexão específicas do driver * */ private function __construct() { } public static function getInstance() { if ( self::$instance === null ) { self::$instance = new PDO(DB_CONN, DB_USER, DB_PASS); } return self::$instance; } } class Model { protected $TABLE_NAME; protected $TABLE_PREFIX; protected $clausula; private $storage; /** * Recupera um registro utilizando sua chave * * @param string $key * * @return mixed O valor armazenado * @throws RuntimeException Se não houver um registro para a chave especificada */ public function __get( $key ) { } /** * Registra um valor à uma chave * * @param string $key A chave que será utilizada no registro * @param mixed $value O valor que será registrado * * @throws LogicException Se a chave já estiver registrada */ public function __set( $key , $value ) { //echo $key; } public static function __callStatic( $method , $args ) { $database = DataBase::getInstance(); $callback = array( $database , $method ); return call_user_func_array( $callback , $args ); } public function __call( $method , $args ) { $database = DataBase::getInstance(); $callback = array( $database , $method ); return call_user_func_array( $callback , $args ); } public function __construct( $table_name = null , $id = null ) { $this-&gt;TABLE_PREFIX = $this-&gt;config['database']['table_prefix']; $this-&gt;TABLE_NAME = $this-&gt;TABLE_PREFIX . $table_name; $this-&gt;storage = new ArrayObject(); if ( !is_null( $table_name ) ) { $array = $this-&gt;query( "SHOW COLUMNS FROM `$this-&gt;TABLE_NAME`" )-&gt;fetchAll(); $colunas = array(); $obrigatorias = array(); foreach ( $array as $value ) { $colunas[] = $value[0]; if ( $value['Null'] === 'NO' ) { $obrigatorias[] = $value['Field']; } } $this-&gt;colunas = $colunas; $this-&gt;obrigatorias = $obrigatorias; $this-&gt;total_colunas = count( $this-&gt;colunas ); // Se passou um id por parâmetro, salva nas propriedades do objeto if ( !is_null( $id ) AND is_numeric( $id ) ) { $this-&gt;id = $id; // E já carrega o objeto $select = $this-&gt;query( 'SELECT * FROM {tabela_nome} WHERE `id` = ' . $id )-&gt;fetchObject(); $this-&gt;load( $select ); } } } public function insert() { } public function update() { } public function delete() { } public function select( $clausula = NULL , $is_array = FALSE ) { // Caso seja passado uma cláusula diretamente para a função, executa ela if ( !is_null( $clausula ) ) { $this-&gt;clausula = $clausula; } // Troca uma possível variável pelo nome da tabela do Model $this-&gt;clausula = ( str_replace( '{TABLE_NAME}' , $this-&gt;TABLE_NAME , $this-&gt;clausula ) ); $this-&gt;clausula = ( str_replace( '{TABLE_PREFIX}' , $this-&gt;TABLE_PREFIX , $this-&gt;clausula ) ); // Executa o SELECT no banco de dados $query = $this-&gt;query( $this-&gt;clausula ); if ( $query AND $query-&gt;rowCount() &gt; 0 ) { if ( $query-&gt;rowCount() == 1 AND !$is_array ) { return $query-&gt;fetchObject( get_class( $this ) ); } else { $objetos = array(); while ( $linha = $query-&gt;fetchObject( get_class( $this ) ) ) { $objetos[] = $linha; } return ( count( $objetos ) &gt; 0 ) ? $objetos : FALSE; } } else { return FALSE; } } public function selectAll() { } public function load() { } } $model = new Model(); $stmt = $model-&gt;query(); $fetch = $stmt-&gt;fetchAll(); var_dump($fetch); </code></pre> <p>This is not tested. But it should give you an idea how to solve the problem. Try this approach. </p> <pre><code>define('DB_TYPE', 'DB_Class_One'); class DB_Class_One extends PDO { public function getData() { print 'Class One'; } } class DB_Class_Two extends PDO { public function getData() { print 'Class Two'; } } class DB_Class_Three extends PDO { public function getData() { print 'Class Three'; } } class DataBase { private static $instance = null; private function __construct() { } private function __clone() { } public static function getInstance() { $class = DB_TYPE; if (self::$instance === null) { self::$instance = new $class("mysql:host=;dbname=", '', ''); } return self::$instance; } } $db = DataBase::getInstance(); $stmt = $db-&gt;query(); $result = $stmt-&gt;fetch(); $db-&gt;getData(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload