How can I initiate a PHP class and use it in sever

2020-03-06 07:51发布

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

标签: php oop class
3条回答
Melony?
2楼-- · 2020-03-06 08:20

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.

查看更多
混吃等死
3楼-- · 2020-03-06 08:26

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 speculating

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!
    }
}
查看更多
祖国的老花朵
4楼-- · 2020-03-06 08:34

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();
    }

}
查看更多
登录 后发表回答