Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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 );
    }
}
share|improve this question

1 Answer 1

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.