2
\$\begingroup\$

I am learning OOP using PHP5 and I wrote two simple classes for receiving data from a database using the dependency injection pattern and rendering a grid with Twig.

Does this code have sense for OOP logic?

index.php

<?php
 /*
 * Remplazar con autoload
 */
require_once 'class/class.db.php';
require_once 'class/class.entradas.php';
require_once 'vendor/autoload.php'; 
Twig_Autoloader::register();
$_db = new Db('localhost', 'root', '', 'tgsm_ascm');
$entradas = new Entradas( $_db );
$arEntradas = $entradas->obtenerUltimasEntradas();
$loader = new Twig_Loader_Filesystem('tpl/');
$twig = new Twig_Environment($loader);
$template = $twig->loadTemplate('ctbaGrillaActividades.twig');
echo $template->render( array( 'arEntradas' => $arEntradas ) );

class.db.php

<?php
class Db
{
 protected $db = NULL;
 private $dbServidor,
 $dbUsuario,
 $dbClave,
 $dbNombre;
 /*
 * Constructor
 * @args string $dbServidor
 * @args string $dbUsuario
 * @args string $dbClave
 * @args string $dbNombre
 * @return null
 */
 public function __construct( $dbServidor, $dbUsuario, $dbClave, $dbNombre )
 {
 $this->dbServidor = $dbServidor;
 $this->dbUsuario = $dbUsuario;
 $this->dbNombre = $dbNombre;
 $this->dbClave = $dbClave;
 if( is_null( $this->db ) )
 {
 $this->conectar( $this->dbServidor, $this->dbUsuario, $this->dbClave, $this->dbNombre );
 }
 }
 /*
 * Conecta a la BD
 * @args string $dbServidor
 * @args string $dbUsuario
 * @args string $dbClave
 * @args string $dbNombre
 * @return null
 */
 public function conectar( $dbServidor="localhost", $dbUsuario="root", $dbClave="", $dbNombre="" )
 {
 $dsn = "mysql:dbname={$dbNombre};host={$dbServidor}";
 try
 {
 $this->db = new PDO( $dsn, $dbUsuario, $dbClave );
 }
 catch ( PDOException $e )
 {
 echo 'Falló la conexión: ' . $e->getMessage();
 } 
 }
 /*
 * Desconecta de la DB
 * @return null
 */
 public function desconectar( )
 {
 $this->db = NULL;
 }
 /*
 * Ejecuta una consulta directa
 * @args string $consulta
 * @args string $fetchOrFetchAll
 * @args string $placeHolder
 * @args string $retunrDataType
 */ 
 public function consultaDirecta( $consulta, $fetchOrFetchAll='fetch', $placeHolder='', $returnDataType=PDO::FETCH_ASSOC )
 {
 if( is_array( $placeHolder ) )
 {
 $p = $this->db->prepare( $consulta ); 
 $p->setFetchMode( $returnDataType );
 if( $p->execute($placeHolder) ) {
 return ($fetchOrFetchAll=='fetch') ? $p->fetch() : $p->fetchAll();
 } else {
 return false;
 }
 }
 else
 {
 $p = $this->db->prepare( $consulta ); 
 $p->setFetchMode( $returnDataType );
 if( $p->execute() ) {
 return ($fetchOrFetchAll=='fetch') ? $p->fetch() : $p->fetchAll();
 } else {
 return false;
 }
 }
 }
}?>

class.entradas.php

<?php
class Entradas
{
 private $db = NULL;
 public function __construct(Db $db)
 {
 $this->db = $db;
 }
 public function obtenerUltimasEntradas()
 {
 $_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->consultaDirecta( $_sql, 'fetchAll', '' );
 }
 /*
 * Debug
 * @return string
 */
 public function debug()
 {
 return print_r( $this->db );
 }
}
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Feb 4, 2013 at 2:37
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

There is one major issue with your code: It's not English

You can easily guess that I'm also no native English speaker, but you can't write methods and variables in your native language. At some place, either in your company, your open source project or finally if you ask for a code review there will be people who don't understand your code because they don't understand your language.

Beside that your questions seems totally unrelated to the template engine, or did I miss anything.

From OOP point of view, you should think about using any of the PHP persistence frameworks and map a database row to an object and not to all objects.

jsanc623
2,86416 silver badges22 bronze badges
answered Feb 4, 2013 at 10:42
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.