【问题标题】:Using classes outside of the current directory in PHP在 PHP 中使用当前目录之外的类
【发布时间】:2014-02-04 19:57:22
【问题描述】:

我的目录中有两个文件夹:

  • 插件

Plugins 文件夹包含两个文件:Sample.php 和 Plugins.php。

Sample.php 只是一个具有一个扩展插件类的函数的类。 Plugins 类尝试创建位于 Classes 文件夹中的基类的新实例。

插件/Sample.php:

class Sample extends Plugins {

    public $eggs;
    public $pounds;

    public function __construct() {
        $this->eggs   = "100";
        $this->pounds = "10";
    }

    public function OnCall() {
        echo "{$this->eggs} eggs cost {$this->pounds} pounds, {$this->name}!";
    }
}

插件/Plugins.php:

class Plugins {

    public $name;

    public function __construct() {
        include '../Classes/Base.php';
        $base = new Base();
        $this->name = $base->name;
    }
}

类/Base.php:

class Base {

    public $name = "Will";

    public function Say() {
        echo $this->name;
    }

}

Index.php 包含 Plugins 文件夹中的所有内容,并且应该执行 OnCall()。它给出以下错误消息:

警告:include(../Classes/Base.php) [function.include]: 失败 打开流:中没有这样的文件或目录 /Applications/XAMPP/xamppfiles/htdocs/Plugins/Plugins/Plugins.php 第 6 行

警告:include() [function.include]:打开失败 '../Classes/Base.php' 用于包含 (include_path='.:/Applications/XAMPP/xamppfiles/lib/php:/Applications/XAMPP/xamppfiles/lib/php/pear') 在 /Applications/XAMPP/xamppfiles/htdocs/Plugins/Plugins/Plugins.php 在第 6 行

致命错误:在 /Applications/XAMPP/xamppfiles/htdocs/Plugins/Plugins/Plugins.php 第 7 行

Index.php(如果有帮助的话):

foreach(glob('Plugins/*.php') as $file) {
    require_once $file;
    $class = basename($file, '.php');
    if(class_exists($class)) {
        $obj = new $class;
        $obj->OnCall();
    }
}

我需要做的是在 Classes 文件夹之外的类中使用 Base 类。我该怎么做?

【问题讨论】:

  • index.php 的代码在哪里?如果这是显示错误并加载类的脚本,我们需要查看它。
  • @helion3 哎呀。现在将它与错误消息一起添加。
  • 尝试使用Classes/Base.php 的绝对路径而不是相对路径。
  • 我已经修复了包含错误,但它仍然不会显示名称变量。
  • 我通过将 require_once 'Classes/Base.php'; $GLOBALS['base'] = new Base(); 添加到 index.php 并在 Plugins.php 中将 $this->base 更改为 $GLOBALS['base']$this->name$this->base->name 来修复它。它现在输出 100 eggs cost 10 pounds, the 100 year old! 而不是 100 eggs cost 10 pounds, Will the 100 year old!

标签: php class directory


【解决方案1】:

您需要在 Sample 类中调用父级的构造函数。

class Sample extends Plugins {

    public $eggs;
    public $pounds;

    public function __construct() {
        parent::__construct();
        $this->eggs   = "100";
        $this->pounds = "10";
    }

    public function OnCall() {
        echo "{$this->eggs} eggs cost {$this->pounds} pounds, {$this->name}!";
    }
}

【讨论】:

  • 谢谢,但我似乎收到了同样的错误。
【解决方案2】:

您可能想利用__autoload (http://ca1.php.net/manual/en/function.autoload.php)

使用此功能可以让您轻松加载类,无论它们位于哪个目录。

简单示例:

function __autoload($classname) {
  $path = "path/to/Classes/$classname.php";
  if(file_exists($path)) {
    require_once $path;
  }
}

这意味着您将能够从您的插件类中删除您的 include 语句,并简单地保留声明 $base = new Base();__autoload 将被神奇地调用并加载正确的文件。

【讨论】:

猜你喜欢
  • 2017-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-26
相关资源
最近更新 更多