i'm trying use database class (adodb php) don't know how use in class. example:
<?php include_once("adodb.inc.php"); $conn = newadoconnection('mysql'); class contacts { public function getdata(){ $conn->connect(...); //do } } ?> i think problem can not call $conn because defined outside class.
notice: undefined variable: conn
fatal error: call member function connect() on non-object
maybe i'm doing wrong way, don't know how fix this.
can please me out? lot!
why not ?!
<?php include_once("adodb.inc.php"); $conn = newadoconnection('mysql'); $conn->connect(...); class contacts { protected $_connection; public function __construct($conn) { $this->_connection = $conn; } public function getdata(){ //do } } $contacts = new contacts($conn); $foobar = new foobar($conn); ?> it makes sense establish connection outside class, , pass in workable object. , of-course share same connection object among classes require it.
p.s reallly recommend drop adodb , start learning pdo.
Comments
Post a Comment