How can I initiate a PHP class and use it across multiple files?

I'm stumped right now. In my last post on this question, the answer was to use a singleton to make sure the object only runs 1 time, but I have the opposite problem.

If I have a file called index.php and then I include these files in 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 test files I will try to access the get () method from the session class , suppose the session class file is already included in the index.php page.

Now if I try to use ...

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

      

in any of the test class files i get this error

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

      

the only way the code works without error is to use

$session = new Session(); 

      

in each file.

How can I fix / avoid having to initialize the class in each file when it is already running in the index.php file?

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

+2


source to share


3 answers


Not seeing the code, which is hard to tell, but I think I can make some assumptions. correct me if i am wrong:

EDIT: So please post your source so we can stop sepculating

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

class a
{
    function a(){}

    function b()
    {

    }
}

      

2) you are not trying to execute code in your class files at load time, but later by creating them

i.e.



require("class.a.php");

$myA = new a();

$a->b();

      

If you try to reference your session variable inside these classes, you have a scoping issue. A variable declared outside of a class definition cannot be used inside a 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 this, but instead you must traverse the session as a variable if the class needs to access it:

class a
{
    function a(){}

    function b($session)
    {
        $session->doSomething(); // yay!
    }
}

      

+5


source


You can have a base class, they all range from

Example



class test1 extends Base {

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

}

class Base {

    protected session;

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

}

      

+1


source


You kind of think about it back. Any file that will use the session object will need to contain a file that contains this class definition. An alternative is to use __autoload to pull the class into:

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

      

EDIT: you need to put the file containing this autoload in every file that will use it.

-1


source







All Articles