在 Joomla 中包含 PHP 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5644846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Including PHP file in Joomla
提问by newbie
I have a logger class that I need to log my application processing. How can I include this file in Joomla, when my file is located in the includes folder. I don't want to always have to include the file using ../../../, because then I have to manually count paths everytime I include my class. Is there any way to generate the line that will include that file relatively?
我有一个记录器类,我需要记录我的应用程序处理。当我的文件位于包含文件夹中时,如何在 Joomla 中包含此文件。我不想总是使用 ../../../ 包含文件,因为这样我每次包含我的类时都必须手动计算路径。有什么方法可以生成相对包含该文件的行吗?
回答by Martin
You could use the Joomla constants for JPath from here: http://docs.joomla.org/JPathlike so:
您可以从这里对 JPath 使用 Joomla 常量:http: //docs.joomla.org/JPath,如下所示:
include_once (JPATH_ROOT.DS.'includes'.DS.'some.class.php');
You can replace JPATH_ROOT with any of the following:
您可以将 JPATH_ROOT 替换为以下任何一项:
JPATH_SITE
JPATH_BASE
回答by Robert Went
In joomlla 2.5 + never use include_once or require_once to include a class file. PHP has to check if the file has already been loaded before loading the file.
在 joomlla 2.5 + 永远不要使用 include_once 或 require_once 来包含类文件。PHP 必须在加载文件之前检查文件是否已经加载。
Since joomla 2.5 there is the JLoader::register() method which doesn't use any memory unless you need to execute a method within the class.
从 joomla 2.5 开始,JLoader::register() 方法不使用任何内存,除非您需要在类中执行一个方法。
回答by Mohammad Efazati
use this every where
到处都用这个
<?php
include $_SERVER['DOCUMENT_ROOT']."/lib/sample.lib.php";
?>