Jump to content

Calling Something "Autoload"ed...


HappyFeet

Recommended Posts

Evening,

 

So I have three files.

 

run.php

// autoload the classes
function __autoload($className) {
    include "class.".$className.".php";
}

// setup the sessions handler
$session = new Session($member);

// setup the handler
$handler = new Handler(part, template, $_GET);

 

The handler takes the $_GET data and includes the templates header -> $_GET matching body file -> footer and returns it forming a page.

 

However. When I go to call something from the Session class for example in the body file with...

 

body.tpl.php

<?=$Session->isLog();?>

 

I get...

 

Notice: Undefined variable: Session in .../body.tpl.php on line 12 Fatal error: Call to a member function isLog() on a non-object in .../body.tpl.php on line 12

 

It is my understanding that the class is ready for use, but isn't?

 

I'd really appreciate any advice you can spare.

Link to comment
Share on other sites

I've never used autoload, but the way I understand it, the code in your first block should fire it:

 

// setup the sessions handler
$session = new Session($member);

and should autoload the Session class (in class.Session.php) creating an object which is stored in the variable $session (lower case name). You could just as easily call it $abc it should not matter. However, you are referring to it in the second code block as

 

<?=$Session->isLog();?>

using an uppercase name ($Session). The error message says

 

Notice: Undefined variable: Session in .../body.tpl.php on line 12

which is saying that the variable $Session (uppercase name) does not exist. If you change that to be the lowercase name ($session), you should not get the same error message -- does it then tell you that $session (lowercase name) does not exist?

 

We don't see enough of the code to know if there is a scope issue here. The object you created exists in whatever scope (Global or function or class) that it is instantiated in. So your reference to it must be in the same scope; otherwise, the variable does not exist (in scope).

 

Have you tested to see if the object is, in fact, being instantiated?

// setup the sessions handler
$session = new Session($member);
if (! is_object($session)) trigger_error('Session Object Not Instantiated', E_USER_ERROR);

although, I would expect you to get an error at that point if it did not.

 

Link to comment
Share on other sites

does it then tell you that $session (lowercase name) does not exist?

 

In fact no, my apologies there! There is no error if you replace the upper case with a lower case 's', but there is nothing returned from the class.

 

Have you tested to see if the object is, in fact, being instantiated?

 

It is being initiated, there are no errors from what you posted.

 

Currently the "class.Session.php" file is simply:

 

class Session {

    public function __construct(){
       
        // Nothing yet.
        
    }
    
    /**
     * Logged in?
     */
    public function isLog(){
                
        if(isset($_SESSION['memberID'])){
            
            return "YES";
            
        } else {
            
            return "NO";
            
        }
            
    }

    
}

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.