stevengreen22 Posted July 22, 2013 Share Posted July 22, 2013 I have : session_start(); spl_autoload_register(null, false); spl_autoload_extensions(".class.php"); function classLoader($class){ $filename = strtolower($class) .'.class.php'; $file = 'core/classes/' .$filename; if(!file_exists($file)){ return false; } require $file; } function connectLoader($connect){ $filename = strtolower($connect) .'.class.php'; $file = 'core/connect/' .$filename; if(!file_exists($file)){ return false; } require $file; } spl_autoload_register('connectLoader'); spl_autoload_register('classLoader'); In an init.php that is called at the top of each page. I keep receiving errors with regards to non member objects when trying to access the site. Fatal error: Call to a member function prepare() on a non-object in /home/a6696695/public_html/core/classes/users.class.php on line 185 on that line is a function that retrieves user data from the database: //function to return the user data public function userData($id){ $query = $this->db->prepare("SELECT * FROM users WHERE id =?"); $query->bindValue(1, $id); try{ $query->execute(); //returns all user data in the form of an array - access in other pages, index etc. return $query->fetch(); }catch(PDOException $e){ die($e->getMessage); } }//end userData function This function is called in the init after the spl calls. //instantiating the classes $users = new Users($db); $admin = new Admin($db); $general = new General(); $helper = new Helper(); //check if user is logged in, get id from session and data if($general->loggedIn() === true){ $userId = $_SESSION['id']; $user = $users->userData($userId); } Help please Quote Link to comment https://forums.phpfreaks.com/topic/280397-sol_autload-woes/ Share on other sites More sharing options...
AbraCadaver Posted July 22, 2013 Share Posted July 22, 2013 Before you do this: $users = new Users($db); You must have something like this somewhere: $db = new SomeDBclass(/* some args maybe */); And before this is available in userData() $this->db You need to assign $db that was passed into the constructor of the User class: public function __construct($db) { $this->db = $db; } So check all of those. Quote Link to comment https://forums.phpfreaks.com/topic/280397-sol_autload-woes/#findComment-1441672 Share on other sites More sharing options...
AbraCadaver Posted July 22, 2013 Share Posted July 22, 2013 (edited) Also, this (or something similar) would be faster and easier than having two or more autoloaders that only differ in the path: set_include_path(get_include_path().PATH_SEPARATOR.'/core/classes'.PATH_SEPARATOR.'/core/connect'); spl_autoload_extensions('.class.php'); spl_autoload_register(); Edited July 22, 2013 by AbraCadaver Quote Link to comment https://forums.phpfreaks.com/topic/280397-sol_autload-woes/#findComment-1441675 Share on other sites More sharing options...
stevengreen22 Posted July 22, 2013 Author Share Posted July 22, 2013 Hi, yup, checked all of those and they're all fine. I've tried various tests and so far have come up with this result... If I include the dbconnect file using require before I call the spl_autoload it works. I tried putting the dbconnect file into the same folder as the classes to test - it failed there too. Quote Link to comment https://forums.phpfreaks.com/topic/280397-sol_autload-woes/#findComment-1441676 Share on other sites More sharing options...
AbraCadaver Posted July 22, 2013 Share Posted July 22, 2013 Hi, yup, checked all of those and they're all fine. I've tried various tests and so far have come up with this result... If I include the dbconnect file using require before I call the spl_autoload it works. I tried putting the dbconnect file into the same folder as the classes to test - it failed there too. So what's the file name and class name of your DB class? Quote Link to comment https://forums.phpfreaks.com/topic/280397-sol_autload-woes/#findComment-1441677 Share on other sites More sharing options...
stevengreen22 Posted July 22, 2013 Author Share Posted July 22, 2013 So what's the file name and class name of your DB class? Hi, It was dbConnect.php. I then tried dbconnect.php and dbconnect.class.php. It's now dbconnect.php again Quote Link to comment https://forums.phpfreaks.com/topic/280397-sol_autload-woes/#findComment-1441678 Share on other sites More sharing options...
AbraCadaver Posted July 22, 2013 Share Posted July 22, 2013 Well the file name must be: dbconnect.class.php and the class name must be dbconnect or dbConnect (or whatever case as long as the class is exactly that name) for your autoloaders to work. Quote Link to comment https://forums.phpfreaks.com/topic/280397-sol_autload-woes/#findComment-1441679 Share on other sites More sharing options...
stevengreen22 Posted July 22, 2013 Author Share Posted July 22, 2013 Well the file name must be: dbconnect.class.php and the class name must be dbconnect or dbConnect (or whatever case as long as the class is exactly that name) for your autoloaders to work. oh ok, changed to .class.php. In my dbconnect.class.php I only have : $config = array( host etc details ); $db = new PDO('mysql:host=' .$config['host'] . ';dbname=' .$config['dbname'], $config['username'], $config['password']); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); Is there any problems there? when you mentioned class name and file name I got a touch confused. This doesn't have typical class structure Quote Link to comment https://forums.phpfreaks.com/topic/280397-sol_autload-woes/#findComment-1441680 Share on other sites More sharing options...
AbraCadaver Posted July 22, 2013 Share Posted July 22, 2013 (edited) There is no class definition in that file and you haven't attempted to use a class that would prompt this file to be autoloaded. An autoloader loads a file to include a class definition when a non-existent class is referenced. Just include this file at the beginning of your script. Edited July 22, 2013 by AbraCadaver Quote Link to comment https://forums.phpfreaks.com/topic/280397-sol_autload-woes/#findComment-1441682 Share on other sites More sharing options...
stevengreen22 Posted July 22, 2013 Author Share Posted July 22, 2013 There is no class definition in that file and you haven't attempted to use a class that would prompt this file to be autoloaded. An autoloader loads a file to include a class definition when a non-existent class is referenced. Just include this file at the beginning of your script. OK, I think I was getting confused. I was trying to cirumnavigate the problems for when I try and access the includes from a different folder in the root. 2 levels down for example and I've just realised that this doesn't actually solve that. Would $file = dirname(__FILE__) be on the right track to solve? Quote Link to comment https://forums.phpfreaks.com/topic/280397-sol_autload-woes/#findComment-1441692 Share on other sites More sharing options...
AbraCadaver Posted July 23, 2013 Share Posted July 23, 2013 Well if you have a main file in the root of your application that is always loaded then you could define the highest level dir and use that and/or add it to the include path: define('ROOTDIR', dirname(__FILE__)); ini_set('include_path', ROOTDIR . DIRECTORY_SEPARATOR . PATH_SEPARATOR . ini_get('include_path')); Then in other files if you want to include, you can do it relative to the root directory. Quote Link to comment https://forums.phpfreaks.com/topic/280397-sol_autload-woes/#findComment-1441788 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.