简体   繁体   中英

How to include external PHP classes in Joomla

I have few PHP classes and files that I want to include in Joomla so that I can call those classes. I tried doing require_once config.php to include a PHP file which also includes all the classes that I would want to use in Joomla.

But each time I execute the page I receive the error below:

 Fatal error: Class 'SomeClassName' not found

is there any other way to include external PHP classes or files in Joomla?

Thanks in advance!

Please use Joomla autoloader. Is better.

<?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);

Edit :

Load classes with autoload instead of require/include have a better performance, because PHP will only read (require access to the disk) and compile (require memory and CPU usage) if you really use your class.

To do the same with require/include you have to be sure to only use if will really use the class.


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

require_once should work just fine within Joomla. Make sure the class you want to use is really loaded within your file, and the file is properly referenced in the require_once . Something is going wrong there and it has nothing to do with Joomla itself :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM