Including PHP file in Joomla

30,533

Solution 1

You could use the Joomla constants for JPath from here: http://docs.joomla.org/JPath like so:

include_once (JPATH_ROOT.DS.'includes'.DS.'some.class.php');

You can replace JPATH_ROOT with any of the following:

JPATH_SITE 
JPATH_BASE 

Solution 2

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.

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.

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

Solution 3

use this every where

<?php
include $_SERVER['DOCUMENT_ROOT']."/lib/sample.lib.php";
?>
Share:
30,533

Related videos on Youtube

newbie
Author by

newbie

Java developer

Updated on July 09, 2022

Comments

  • newbie
    newbie over 1 year

    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?

    • Some Newbie
      Some Newbie over 9 years
      I find this "close" as very unconstructive. There are constructive responses below though. It seems like some people get high on abusing their karma point privileges.
  • Valentin Despa
    Valentin Despa almost 11 years
    Please note that DS is deprecated in 2.5 and removed in 3.0.
  • bumerang
    bumerang over 9 years
    You are right @Valentin Despa in Joomla 3 it is DIRECTORY_SEPARATOR
  • Valentin Despa
    Valentin Despa over 9 years
    You don't even need it. Just use /

Related