php - Trying to use a variable over multiple classes -


at moment, initiating db connectiong via each function in mvc so:

<?php  class system extends controller {      public static function get($info = '')     {         try         {             if($info):                  $database = databasefactory::getfactory()->getconnection();                  $get_info = $database->prepare("select * site"); //get info datbase                  $result = $get_info->execute(array($info)); //run query                  $information = $result->fetch(); //fetch information                  return $information->$info; //return info use on other parts of site             else:                 throw new exception("there error selecting site information");             endif;          }catch(exception $e)         {             echo $e->getmessage();         }     } }  ?> 

which means need use

$database = databasefactory::getfactory()->getconnection(); 

in each , every function. trying initiate database variable in controller (my controller extends classes) can $database->prepare rather initiate in each function. way this>

i have tried

var $database = databasefactory::getfactory()->getconnection(); 

and

public $database = databasefactory::getfactory()->getconnection(); 

with no avail.

you can't during initialization because member variable initialization must static, , you're trying call function. the manual:

this declaration may include initialization, initialization must constant value--that is, must able evaluated @ compile time , must not depend on run-time information in order evaluated.

instead, in constructor:

class controller {     protected $database;     function __construct() {         $this->database = databasefactory::getfactory()->getconnection();     } } 

Comments