Skip to content

Instantly share code, notes, and snippets.

@makamo
Created January 26, 2015 16:15
Show Gist options
  • Save makamo/b3f7df11906c7013349b to your computer and use it in GitHub Desktop.
Save makamo/b3f7df11906c7013349b to your computer and use it in GitHub Desktop.
J'ai cette erreur la ( ! ) Fatal error: Using $this when not in object context in /var/www/src/Equipements/Equipements.php on line 12 Call Stack #TimeMemoryFunctionLocation 10.0003237008{main}( ).../index.php:0 20.0058269792App\Equipements\Equipements::getList( ).../index.php:51
<?php
namespace App\Equipements;
use App\Connection\Connection;
class Equipements extends Connection{
private $table = '"TAB_Equipement"';
public static function getList(){
$query = "SELECT * FROM ".$this->getTable();
return Parent::getInstance()->fetchAllObj($query);
}
public function getTable()
{
return $this->table;
}
}
@makamo
Copy link
Author

makamo commented Jan 26, 2015

namespace App\Connection;

use PDO;

class Connection {

    /**
     * Instance de la classe Connection
     *
     * @var Connection
     * @access private
     */
    private $PDOInstance = null;

    /**
     * Instance de la classe SPDO
     *
     * @var Connection
     * @access private
     * @static
     */
    private static $instance = null;

    private  $DSN = "odbc:boreal";

    public $Error;

    /**
     * Constructeur
     *
     * @internal param $void
     * @see PDO::__construct()
     */
    public function __construct()
    {
        try{
            $this->PDOInstance = new PDO($this->DSN);
            $this->PDOInstance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }catch(PDOException  $e){
            $this->Error = $e->getMessage();
        }
    }


    /**
     * Crée et retourne l'objet SPDO
     *
     * @access public
     * @static
     * @param void
     * @return Connection $instance
     */
    public static function getInstance()
    {
        if(is_null(self::$instance))
        {
            self::$instance = new Connection();
        }
        return self::$instance;
    }

    /**
     * Exécute une requête SQL avec PDO
     *
     * @param string $query La requête SQL
     * @return PDOStatement Retourne l'objet PDOStatement
     */
    public function query($query)
    {
        return $this->PDOInstance->query($query);
    }

    /**
     * Exécute une requête SQL avec PDO
     *
     * @param string $query La requête SQL
     * @return PDOStatement Retourne l'objet PDOStatement
     */
    public function fetchAllObj($query)
    {
        return $this->PDOInstance->query($query)->fetchAll(PDO::FETCH_OBJ);
    }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment