I am learning OOP using PHP5 and I wrote two simple classes for receive data of database using dependency injection pattern and render a grid with Twig. Please, if is possible, take a look and give me any feedback.
My question is: does this code have sense for OOP logic?
Sorry About my English
index.php
<?php
/*
* Replace with autoload
*/
require_once 'class/class.db.php';
require_once 'class/class.entries.php';
require_once 'vendor/autoload.php';
Twig_Autoloader::register();
$_db = new Db('localhost', 'root', '', 'tgsm_ascm');
$entries = new Entries( $_db );
$arEntries = $entries->getLastEntries();
$loader = new Twig_Loader_Filesystem('tpl/');
$twig = new Twig_Environment($loader);
$template = $twig->loadTemplate('ctbaGrillaActividades.twig');
echo $template->render( array( 'arEntries' => $arEntries ) );
?>
class.db.php
<?php
/*
* Database manager
* @autor Rebolini Pablo Demian <[email protected]>
*/
class Db
{
protected $db = NULL;
private $dbServer,
$dbUser,
$dbPassword,
$dbName;
/*
* Constuct
* @args string $dbSrv
* @args string $dbUsr
* @args string $dbPsw
* @args string $dbName
* @return null
*/
public function __construct( $dbSrv, $dbUsr, $dbPsw, $dbName )
{
$this->dbServer = $dbSrv;
$this->dbUser = $dbUsr;
$this->dbName = $dbName;
$this->dbPassword = $dbPsw;
if( is_null( $this->db ) )
{
$this->conect( $this->dbServer, $this->dbUser, $this->dbPassword, $this->dbName );
}
}
/*
* Conecta a la BD
* @args string $dbServidor
* @args string $dbUsuario
* @args string $dbClave
* @args string $dbNombre
* @return null
*/
public function conect( $dbSrv="localhost", $dbUsr="root", $dbPws="", $dbName="tgsm_ascm" )
{
$dsn = "mysql:dbname={$dbName};host={$dbSrv}";
try
{
$this->db = new PDO( $dsn, $dbUsr, $dbPws );
}
catch ( PDOException $e )
{
echo 'Falló la conexión: ' . $e->getMessage();
}
}
/*
* Desconecta de la DB
* @return null
*/
public function disconect( )
{
$this->db = NULL;
}
/*
* Execute Direct SQL Queries
* @args string $query
* @args string $fetchOrFetchAll
* @args string $placeHolder
* @args string $retunrDataType
*/
public function directQuery( $query, $fetchOrFetchAll='fetch', $placeHolder='', $returnDataType=PDO::FETCH_ASSOC )
{
if( is_array( $placeHolder ) )
{
$p = $this->db->prepare( $query );
$p->setFetchMode( $returnDataType );
if( $p->execute($placeHolder) ) {
return ($fetchOrFetchAll=='fetch') ? $p->fetch() : $p->fetchAll();
} else {
return false;
}
}
else
{
$p = $this->db->prepare( $query );
$p->setFetchMode( $returnDataType );
if( $p->execute() ) {
return ($fetchOrFetchAll=='fetch') ? $p->fetch() : $p->fetchAll();
} else {
return false;
}
}
}
}
?>
class.entradas.php
<?php
/*
* Clase Maestra de manejo de entradas
* @autor Rebolini Pablo Demian <[email protected]>
*/
class Entries
{
private $db = NULL;
public function __construct(Db $db)
{
$this->db = $db;
}
public function getLastEntries()
{
$_sql = "SELECT ascm_entradas.ID,
ascm_entradas.autor,
ascm_entradas.titulo,
ascm_entradas.leyenda,
ascm_entradas.img_miniatura
FROM
ascm_entradas
WHERE
ascm_entradas.`status` = '2'
ORDER BY
ascm_entradas.ID DESC
LIMIT 0, 15";
return $this->db->directQuery( $_sql, 'fetchAll', '' );
}
/*
* Debug
* @return string
*/
public function debug()
{
return print_r( $this->db );
}
Thank you