如何启动一个PHP类并在几个文件中使用它? [英] How can I initiate a PHP class and use it in several files?

查看:88
本文介绍了如何启动一个PHP类并在几个文件中使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在很累。在我最后一篇关于这个问题的答案是答案是使用单例,以确保一个对象只启动一次,但我有相反的问题。



如果我有一个名为 index.php 的文件,然后将这些文件包含到 class1.php class2.php class3.php class4.php



index.php 中,我将有

 <?PHP 
$ session = new Session();

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

那么在所有4个测试文件中,我将尝试访问一个名为 get )会话类,假定会话类文件已经包括在index.php页面中。



现在,如果我尝试使用...

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

在任何测试类文件中,我会收到此错误

 致命错误:调用非对象的成员函数get()

如果我使用

  $ session = new Session(); 



在index.php文件中启动时,如何修复/避免在每个文件中初始化类? p>

目标是让我在1个文件中创建一个类,如index.php,然后将类文件包含到该页面中,catch是大多数类使用的方法从其他如果我不必在每个文件中启动每个类,那么这将是很好的。

解决方案

告诉,但我想我可以做一些假设。请纠正我,如果我错了:



编辑:所以发布你的源,所以我们可以停止分隔



1 )您包含的文件是类文件。换句话说,它们包含如下:

  class a 
{
function a(){}

function b()
{

}
}

2)您不是在加载时尝试在类文件中执行代码,而是稍后通过实例化它们



ie

  require(class.a.php); 

$ myA = new a();

$ a-> b();

如果你试图在这些类中引用你的会话变量,那么你有一个范围问题。在类定义之外声明的变量不能在类中使用,除非它在类中声明为全局变量。

  class a 
{
function a(){}

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

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

即使这样,想要这样做,但是如果类需要访问它,你应该将你的会话作为一个变量传入:

  class a 
{
function a(){}

function b($ session)
{
$ session-> doSomething // 好极了!
}
}


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

解决方案

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!
    }
}

这篇关于如何启动一个PHP类并在几个文件中使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆