【发布时间】: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!。