Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Programming Q&A
PHP PDO Singleton usage in other classes
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="BIOS" data-source="post: 373145" data-attributes="member: 15674"><p>You could create functions within the database class to handle all types of data, e.g. query/numrows/fetch functions, that way it would still remain very flexible & less logic would be needed in the other classes as the database logic would be more maintained by the class.</p><p></p><p>Here's how my database class works (in a similar way):</p><p>[PHP] public function initiate(){</p><p> </p><p> if($this->db instanceof PDO){</p><p> return $this->db;</p><p> }</p><p></p><p> }</p><p></p><p> public function rows($query, $bind = []){</p><p></p><p> try{</p><p> </p><p> $this->stmt = $this->db->prepare($query);</p><p> $this->stmt->execute($bind);</p><p> </p><p> $this->result = $this->stmt->rowCount();</p><p> return $this->result;</p><p> </p><p> }catch(PDOException $e){</p><p></p><p> $this->logger->addWarning("SQL error: ".$e);</p><p> exit('Sorry, something happened. Please try again.');</p><p></p><p> }</p><p></p><p> }[/PHP]</p></blockquote><p></p>
[QUOTE="BIOS, post: 373145, member: 15674"] You could create functions within the database class to handle all types of data, e.g. query/numrows/fetch functions, that way it would still remain very flexible & less logic would be needed in the other classes as the database logic would be more maintained by the class. Here's how my database class works (in a similar way): [PHP] public function initiate(){ if($this->db instanceof PDO){ return $this->db; } } public function rows($query, $bind = []){ try{ $this->stmt = $this->db->prepare($query); $this->stmt->execute($bind); $this->result = $this->stmt->rowCount(); return $this->result; }catch(PDOException $e){ $this->logger->addWarning("SQL error: ".$e); exit('Sorry, something happened. Please try again.'); } }[/PHP] [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Programming Q&A
PHP PDO Singleton usage in other classes
Top