king.oslo Posted March 15, 2010 Share Posted March 15, 2010 Good morning, I have this class saved in "class_lib/db.php": <?php class DBManager { protected $_connection; private $_type; function __construct($db, $host, $username, $password) { $this->_type = $db; if ($this->_type == 'mysql') { $this->_connection = mysql_connect($host, $username, $password); mysql_query('SET NAMES UTF8'); mysql_query('SET CHARACTER_SET utf8'); } } function dbRead($query) { if ($this->_type == 'mysql') { $resultat = mysql_query($query, $this->_connection); for ($i = 0; $i < mysql_num_rows($resultat); $i++) { $array[$i] = mysql_fetch_array($resultat, MYSQL_ASSOC); } } return $array; } function dbWrite($query) { if ($this->_type == 'mysql') { $sql = mysql_query($query); } } } I also have this instantiation: <?php include_once('class_lib/db.php'); $DBManager = new DBManager('mysql', 'host.example.com', 'xxxxxxxx', 'xxxxxxxxxxxx'); print '<pre>'; print_r($DBManager -> dbRead('SELECT * FROM db.table')); print '</pre>'; I get this error: "Fatal error: Cannot access empty property in /home/category/class_lib/db.php on line 9" Notice: i only get the error if I try and include the class from a separate file. If I copy the class to the view, I get no error. The question: Why? Quote Link to comment https://forums.phpfreaks.com/topic/195286-class-error-when-included/ Share on other sites More sharing options...
PFMaBiSmAd Posted March 15, 2010 Share Posted March 15, 2010 Your code works for me. Best guess is that the actual code being executed is not what you think it is. You either have more than one class_lib/db.php folder/file and the wrong one is being included (due to the php include_path setting) or when you uploaded the db.php file it did not work and an older version of the code is what is being used. Quote Link to comment https://forums.phpfreaks.com/topic/195286-class-error-when-included/#findComment-1026255 Share on other sites More sharing options...
king.oslo Posted March 15, 2010 Author Share Posted March 15, 2010 Thanks, Yes you were right. M Quote Link to comment https://forums.phpfreaks.com/topic/195286-class-error-when-included/#findComment-1026258 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.