Jump to content

sol_autload woes


stevengreen22

Recommended Posts

  1. I have :

  2.  

    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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by AbraCadaver
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by AbraCadaver
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.