Note that there are some explanatory texts on larger screens.

plurals
  1. PONot able to get a Database instance inside a Model class
    primarykey
    data
    text
    <p>Does a class' __construct() method need to be public, in order to call a static method of this class inside of another class?</p> <p>I'm trying to get a Database instance inside my Model class (MVC app), by calling a static method of the Database class. The constructor of this Database class is private, in order to have a singleton pattern. But it seems to be interfering when I try to call the static method inside my Model class.</p> <p>When I set the Database constructor to public, it works. But then it's not really a singleton pattern anymore.</p> <p>Any suggestions?</p> <p><strong>Database class:</strong></p> <pre><code>class Database extends PDO { private static $instance; private $dsn; private $user; private $pass; public function __construct() { $this-&gt;dsn = DB_ENGINE .':host='. DB_HOST .';dbname='. DB_NAME; $this-&gt;user = DB_USER; $this-&gt;pass = DB_PASS; parent::__construct($this-&gt;dsn, $this-&gt;user, $this-&gt;pass); } public static function getInstance() { if (!isset(self::$instance)) { $object = __CLASS__; self::$instance = new $object; } return self::$instance; } /** * select * @param string $sql An SQL string * @param array $array Paramters to bind * @param constant $fetchMode A PDO Fetch mode * @return mixed */ public function select($sql, $fetchMode = PDO::FETCH_ASSOC, $array = array()) { $stmt = $this-&gt;prepare($sql); foreach ($array as $key =&gt; $value) { $stmt-&gt;bindValue("$key", $value); } $stmt-&gt;execute(); return $stmt-&gt;fetchAll($fetchMode); } /** * insert * @param string $table A name of table to insert into * @param string $data An associative array */ public function insert($table, $data) { ksort($data); $fieldNames = implode(', ', array_keys($data)); $fieldValues = ':' . implode(', :', array_keys($data)); $stmt = $this-&gt;prepare("INSERT INTO $table ($fieldNames) VALUES ($fieldValues)"); foreach ($data as $key =&gt; $value) { $stmt-&gt;bindValue(":$key", $value); } $stmt-&gt;execute(); } /** * update * @param string $table A name of table to insert into * @param string $data An associative array * @param string $where the WHERE query part */ public function update($table, $data, $where) { ksort($data); $fieldDetails = NULL; foreach ($data as $key=&gt; $value) { $fieldDetails .= "$key=:$key,"; } $fieldDetails = rtrim($fieldDetails, ','); $stmt = $this-&gt;prepare("UPDATE $table SET $fieldDetails WHERE $where"); foreach ($data as $key =&gt; $value) { $stmt-&gt;bindValue(":$key", $value); } $stmt-&gt;execute(); } /** * delete * * @param string $table * @param string $where * @param integer $limit * @return integer Affected Rows */ public function delete($table, $where, $limit = 1) { return $this-&gt;exec("DELETE FROM $table WHERE $where LIMIT $limit"); } } </code></pre> <p><strong>Model class:</strong></p> <pre><code>class Model { protected $db; public function __construct() { $this-&gt;db = Database::getInstance(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload