Image

Imagepotatosandwater wrote in Imagephp 🤔curious

php4 class extending and shared variables

update
mysql_pconnect it is.


I have a base class in which I'd like to extend. The thing is, the base class connects to a database. How can I make it so any classes I extend from base class will share the one connection to the database and not make a duplicate connection for each object i make?

example:
class base {
 var $connection;
 function base(){
  $this->connection = mysql_connection("server","user","password");
  mysql_select_db("database",$this->connection);
 }
 function exec($query){
  return mysql_query($query,$this->connection);
 }
}

class child extends base {
 var $foo;
 var $bar;
 function child(){
  $this->foo = mysql_result($this->exec("select foo from foobar"),0);
 }
}


I'd like it so no matter how many objects i make of the child class, they all share the same database connection. I think in PHP5 you can do something like "shared $connection" but i'm using php4.

tia

-ryan