【问题标题】:How to include external PHP classes in Joomla如何在 Joomla 中包含外部 PHP 类
【发布时间】:2013-06-05 04:41:48
【问题描述】:

我想在 Joomla 中包含几个 PHP 类和文件,以便我可以调用这些类。我尝试使用require_once config.php 来包含PHP file,其中还包括我想在Joomla 中使用的所有类。

但每次我执行页面时都会收到以下错误:

 Fatal error: Class 'SomeClassName' not found

有没有其他方法可以在 Joomla 中包含外部 PHP 类或文件?

提前致谢!

【问题讨论】:

  • 尝试编写模块或插件。

标签: php joomla


【解决方案1】:

请使用 Joomla 自动加载器。更好。

<?php
// Register an adhoc class.
JLoader::register('AdhocClass', '/the/path/adhoc.php');

// Register a custom class to override as core class.
// This must be done before the core class is loaded.
JLoader::register('JDatabase', '/custom/path/database_driver.php', true);

编辑

使用自动加载而不是 require/include 加载类具有更好的性能,因为 PHP 只会在您真正使用您的类时读取(需要访问磁盘)和编译(需要内存和 CPU 使用)。

要对 require/include 执行相同操作,您必须确保仅使用 if 将真正使用该类。


来源http://developer.joomla.org/manual/ch01s04.html

【讨论】:

    【解决方案2】:

    require_once 在 Joomla 中应该可以正常工作。确保您要使用的类确实已加载到您的文件中,并且该文件在require_once 中被正确引用。那里出了点问题,与 Joomla 本身无关 :-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-10
      • 1970-01-01
      • 2011-02-15
      • 1970-01-01
      • 2013-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多