papacostas Posted November 30, 2007 Share Posted November 30, 2007 Ok I have a problem which i assume is not that complicated, but it is for me... I have a class called Display that makes calls to a database access class called DAO where all the SQL code is and the logic for fetching the data. I've always been instantiating this class for every function, (like this) require_once(classname) $DAO = new DAO(); which is not very good. so now i want to figure out a way to just do it once in the beginning of the class and then access is whenever i want to i've played around with constructors, autoload and global variables without getting the result i want. what can i do? here is a snippet of the class i currently use, it works fine for fetching single row data but multiple records i will need to access the FetchArray class from the DAO which fetches the mysql array. any comments are greatly appreciated class Display { function getDBArray($function, $id) { $getArray = call_user_func(array($DAO, $function), $id); $return = $DAO->fetchArray($getArray); return $return; } function postDBArray($function, $values) { $postArray = call_user_func(array($DAO, $function), $values); } function getUserInfo($id){ $user = $this->getDBArray('getUserInfo', $id); echo hello $user['name'] ; } } Link to comment https://forums.phpfreaks.com/topic/79584-instantiate-a-class-within-a-class-once/ Share on other sites More sharing options...
trq Posted December 1, 2007 Share Posted December 1, 2007 <?php class a { function foo() { echo "foo"; } } class b { private $a; function __construct() { $this->a = new a; } function bar() { $this->a->foo(); } } ?> Link to comment https://forums.phpfreaks.com/topic/79584-instantiate-a-class-within-a-class-once/#findComment-403392 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.