我的目录结构如下所示
> Root
> -Admin // admin area
> --index.php // admin landing page, it includes ../config.php
> -classes // all classes
> ---template.php
> ---template_vars.php // this file is used inside template.php as $template_vars = new tamplate_vars();
> -templates // all templates in different folder
> --template1
> -index.php
> -config.php
在我的配置中.php我使用过的文件
<?php
.... // some other php code
spl_autoload_register(NULL, FALSE);
spl_autoload_extensions('.php');
spl_autoload_register();
classes'template::setTemplate('template/template1');
classes'template::setMaster('master');
.... // some other php code
?>
我已经设置了正确的命名空间(仅在类中(和索引中.php在根目录上我访问类
<?php
require 'config.php';
$news_array = array('news1', 'news1'); // coming from database
$indexTemplate = new 'classes'template('index');
$indexTemplate->news_list = $news_array; // news_list variable inside index template is magically created and is the object of template_vars class
$indexTemplate->render();
?>
到目前为止,它工作正常,它渲染模板并填充模板变量,
但是当我在管理文件夹中打开索引文件时,它给出了以下错误
致命错误:在 中找不到类"类''template_vars" /home/aamir/www/CMS/classes/template.php 在第 47 行
知道如何解决这个问题。它适用于 root,但从管理面板内部它不起作用
你必须使用一个技巧:
set_include_path(get_include_path() . PATH_SEPARATOR . '../');
在包含../config.php
或
set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__ . '/');
内部config.php
我遇到了同样的问题(在Windows上(,我想我可以解释这个问题。
例如,让我们以两个简单的脚本为例,第一个脚本在名为 root
的目录中创建一个名为 root
的namespace
类:
- root'sampleClass.php
:
namespace root;
class sampleClass
{
public function __construct()
{
echo __CLASS__;
}
public function getNameSpace()
{
return __NAMESPACE__;
}
}
位于此处的第二个脚本root'index.php
,仅包含这两行:
spl_autoload_register();
$o = new sampleClass;
然后,如果您运行root'index.php
脚本,您将收到如下所示的致命错误:
( ! ) SCREAM: Error suppression ignored for
( ! ) Fatal error: spl_autoload(): Class sampleClass could not be loaded in C:'wamp'www'root'test.php on line 4
如果你删除root'sampleClass.php
上的命名空间关键字,错误就会消失。
所以我不会对核心 php 的风度做出任何结论,因为我不是 php 核心开发人员,但会发生这样的事情:
如果您没有指定命名空间,spl_autoload_register();
函数还将查找当前目录(root'
(并找到名为 sampleClass.php
的类,但如果您指定一个命名空间,例如在我们的示例中root
,spl_autoload_register();
函数将尝试加载位于"root''root"之类的文件。
所以在这一点上你有两个解决方案:
1(首先是不合适的是创建一个名为root
的子目录,其中也包含sampleClass.php
2(第二个更好的是在index.php
脚本中加入一种技巧:
set_include_path(get_include_path().PATH_SEPARATOR.realpath('..'));
这将告诉spl_autoload_register()
函数签入父目录。
就这样