tags:

views:

138

answers:

3

I am stumped right now. In my last post about this question the answer was to use a singleton to make sure an object is only initiated 1 time but I am having the opposite problem.

If I have a file called index.php and then I include these files into it, class1.php, class2.php, class3.php, class4.php.

In index.php I will have,

<?PHP
$session = new Session();  

require_once '/includes/class1php';
require_once '/includes/class2.php';
require_once '/includes/class3.php';
require_once '/includes/class4.php';
?>

then in all 4 of the test files I will try to access a method called get() from the session class, assume the session class file is already included into the index.php page as well.

Now if I try to use...

$testvar = $session->get($var1);

in any of the test class files I will get this error

Fatal error: Call to a member function get() on a non-object

the only way the code works without an error is if I use

$session = new Session();

in every file.

How can I fix/avoid having to initaite the class in every file when it is already initated in the index.php file?

the goal is to let me initiate a class in 1 file like index.php and then include the class files into that page, the catch is most of the classes use methods from other classes so would be nice if I didn't have to initiate every class in every file

A: 

You're kind of thinking about it backwards. Any file that will use the session object will need to include the file containing that class definition. The alternative is to use __autoload to pull the class in:

function __autoload($classname)
{
  if ($classname == 'Session')
  {
    include_once 'Session.php';
  }
}

EDIT : you'll need to put the file containing that autoload into every file that will use it.

pzearfoss
I think you misunderstood my question, autoloading is already in place and the classes are already included but there is some sort of scope issue here
jasondavis
A: 

You could have a base class they all all extend from

Example

class test1 extends Base {

    public function doSomething() {
        $this->session->get('something'); 
    }    

}

class Base {

    protected session;

    public function __construct() {
        $this->session = new Session();
    }

}
alex
Can I ask why I was voted down?
alex
+2  A: 

Without seeing the code it's hard to tell, but I think I can make some assumptions. correct me if i'm wrong:

EDIT: So post your source so we can stop sepculating

1) The files you are including are class files. in other words, they contain something like:

class a
{
    function a(){}

    function b()
    {

    }
}

2) you aren't trying to execute code in the class files, at load time, but at some later time by instantiating them

i.e.

require("class.a.php");

$myA = new a();

$a->b();

If you are trying to reference your session variable inside those classes, then you have a scope issue. A variable declared outside a class definition can't be used inside the class, unless it is declared as a global var inside the class.

class a
{
    function a(){}

    function willFail()
    {
        $session->doSomething(); //fails
    }    

    function b()
    {
        global $session;
        $session->doSomething(); //succeeds
    }
}

Even then, you probably don't want to do that, but instead you should pass in your session as a variable if the class needs access to it:

class a
{
    function a(){}

    function b($session)
    {
        $session->doSomething(); // yay!
    }
}
Zak
You understood my question correctly, thanks, the files are all made up so there isn't really anything to post here but you got it correct what I am trying to do. Basicly I have a session class the sets and gets session variables, I then have a user class where I have a method in the user class that I would like to set like 10-15 variables from session variables, this is where I am trying to use my session class inside of my user class
jasondavis
Also the global thing seems to do the trick before it wasn't because I didn't realize the variable was case sensitive
jasondavis