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 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 <rebolini.pablo@gmail.com>
 */
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 <rebolini.pablo@gmail.com>
 */
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

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
    
Thanks for the feedback!... Title and code edited. –  Pablo Rebolini Feb 4 '13 at 16:46

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.