【问题标题】:PHP extends doesn't work.. Class 'Smarty' not foundPHP 扩展不起作用.. 找不到类“Smarty”
【发布时间】:2012-09-19 06:36:21
【问题描述】:

我对 PHP 的 extends 有疑问。我的代码在page.class.php

    <?php
    require_once ('globals.class.php');
    require_once ('Smarty.class.php');

    class CPage extends Smarty
    {
     .
     .
     .

Smarty.class.php 中,类名是Smarty

所以我不知道为什么会出现这个错误:

致命错误:在第 6 行的 C:\xampp\htdocs\blog\www\classes\page.class.php 中找不到类“Smarty”

【问题讨论】:

  • 你确定Smarty.class.php 不为空吗?
  • 如果 Smarty 使用命名空间,你应该声明 class CPage extends \namespace\of\Smarty
  • 你能做一个echo (defined('SMARTY_DIR') ? 'Yes' : 'No'); 看看Smarty.class.php 是否被正确处理了吗?除非没有调用 require_once 或调用“错误”文件(例如,正如 Mihai 所说,一个空文件),否则 hte 代码没有理由失败。
  • echo (defined('SMARTY_DIR') ? 'Yes' : 'No'); -> 答案是“否”.. Smarty.class.php 不是空的,该类将在那里定义 100%。我认为答案应该是“是”@Robbie 还是?怎么了?! ://

标签: php smarty extends


【解决方案1】:

这更像是一个评论而不是一个实际的答案:

当您使用require 时,我假设Smarty 类已被定义。

所以您可能正在使用命名空间,这需要您 use 那个特定的类或提供一个完全限定的类名 (FQCN):

class CPage extends \Smarty
                    ^- Smarty is in the global namespace in this example

否则使用上面的类:

Use \Smarty;

将其导入您当前的命名空间。

答案是前提条件是您引用的Smarty 类位于全局命名空间中。我不熟悉 Smarty,所​​以我不知道它是否有自己的命名空间,如果有,它是哪一个。所以具体的 FQCN 可能会有所不同。

参见:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-22
    • 2012-11-16
    • 2012-01-22
    • 2015-02-19
    • 1970-01-01
    • 1970-01-01
    • 2015-03-04
    • 2012-09-18
    相关资源
    最近更新 更多