PHP OOP accessing global variable -
i have 2 php files. first library , other function problem if declare variable outside class error saying unidentified variable. need in advance.
lib.php
class test{ public function __construct() { $this->_link = mysql_connect('localhost','root',''); mysql_select_db('test_db', $this->_link); } public function query($sql) { } }
function.php
include_once('lib.php'); $lib = new test(); function testfunction(){ $lib->query($sql); }
the problem variable $db
unidentified , don't want type $lib = new test()
every function. in advance.
access variable inside every function using
function whatever($bar) { global $lib; $lib->foo($bar);
or pass $lib parameter
function whatever($lib, $bar) { $lib->foo($bar);
Comments
Post a Comment