![]() |
|
|
|
|
1
4th January 00:18
External User
Posts: 1
|
Many here have said you can't produce a persistent connection, however this is false if you are using objects take below as an example with strict visibility; sorry about no comments just quite busy but hope this helps someone who wants to broaden their perspective;
class A { private $link; public $objects; //load all child objects with the connection public function __construct($classes, $db, $host, $user, $pass) { $this->link = $this->connect($db, $host, $user, $pass); $name = __CLASS__; foreach ($classes as $id => $obj) { if(is_subclass_of($obj,$name)) { $this->objects[$obj] = new $obj($this->link); } } private function connect($db, $host, $user, $pass) { $link = new mysqli($host,$user,$pass,$db); if($link->error) return "could not connect to database server, ". $link->error; else return $link; } public __destruct($void) { if($void) $this->link->close(); } } class B extends A { private $link; public function __construct($link) { $this->link = $link } function getUsers() { $q = $this->link->prepare("select id,name,location from users"); $q->execute(); $q->bind_result($id,$name,$location); $q->fetch(); return array($id,$name,$location); } } example of use: $classes = array('B'); $db = 'mydatabase'; $host = '127.0.0.1'; $user = 'username'; $pass = 'password'; $a = new A($classes, $db, $host, $user, $pass); extract($a->objects, EXTR_PREFIX_SAME, 'obj'); $b->getUsers(); this example allows you to initiate one instance of every object as long as it is a child with the same connection. this is just a snippet of a much bigger model. ---- Server IP: 83.138.144.80 Probable Submitter: 80.229.95.154 ---- Manual Page -- http://www.php.net/manual/en/ref.mysqli.php Edit -- https://master.php.net/note/edit/72083 Del: integrated -- https://master.php.net/note/delete/72083/integrated Del: useless -- https://master.php.net/note/delete/72083/useless Del: bad code -- https://master.php.net/note/delete/72083/bad+code Del: spam -- https://master.php.net/note/delete/72083/spam Del: non-english -- https://master.php.net/note/delete/72083/non-english Del: in docs -- https://master.php.net/note/delete/72083/in+docs Del: other reasons-- https://master.php.net/note/delete/72083 Reject -- https://master.php.net/note/reject/72083 Search -- https://master.php.net/manage/user-notes.php |
|
|
|
|
|
| Some other forums that might be of your interest : Php 5 forum, Apache forum, Iis forum, Functions forum, Classes forum, Librarys forum, Bugs forum, Postgres forum, Mysql forum, Paradox forum, Ms sql forum, Configurations forum, Php.ini forum, Problems forum, Scripting forum, Css forum, General forums, Off-topic talk, Links, Extra forums, Php |