I am developing a web application, using the Codeigniter ( http://codeigniter.com/ ) framework plus the Flourish library (unframework) ( http://flourishlib.com/ ).
I simply dropped the flourish folder into my application, then I created a flourish initialization and config files as instructed (these create the Flourish autoload).
This is my folder structure:
---auxcode\
--------init.php
--------config.php
--------flourish\
---system\
---application\
---public_html\
The init file ONLY includes the config file, and the config file contents shown below:
function __autoload($class_name){
{
// Customize this to your root Flourish directory
$flourish_root = $_SERVER['DOCUMENT_ROOT'] . '/../auxcode/flourish/';
$file = $flourish_root . $class_name . '.php';
if (file_exists($file)) {
include $file;
return;
}
throw new Exception('The class ' . $class_name . ' could not be loaded');
}
In public_html, the index file has been prepended with this:
<?php
include_once($_SERVER['DOCUMENT_ROOT'] . '/../inc/init.php');
Now, the respective autoload functions (as each has its own) are conflicting. The application only works when I comment out the autoload functions (and their dependents) of either framework.
Please how can I merge the autoload functions such that I can access both CI and flourish the same way?
Or if there is a better method to use both systems in one application? Pray, tell.
Thanks.
I'm the author of Flourish. The example autoloader I provide on the getting started page is just supposed to help people get up and started if they don't have an environment already.
In your case since you have multiple libraries, I would recommend using spl_autoload_register(). You can register the CI autoloader and then register your Flourish one.
Create a custom __autoload function. Rename the CI original into __autoload_ci and the Flourish __autoload_flourish.
It's important to add a return true;
to both original autoloaders, when they were successful. Remove any errors/exceptions.
Then deploy a custom wrapper:
function __autoload($class) {
__autoload_ci($class) || __autoload_flourish($class);
}
Or use spl_autoload_register
Thanks to http://codeigniter.com/forums/viewthread/73804/#366081 and some bits of information from some CI folk that I follow on twitter (I asked em): Eric Barnes, Dan Horrigan, Phil Sturgeon and Zack Kitzmiller, I found a solution. If you are a CodeIgniter n00b like me, you may like to follow these guys.
I deleted init.php and config.php, then jammed the following into the bottom of my CI's config.php (I am also autoloading from a custom library called mylibrary).
function multi_auto_require($class) {
if(stripos($class, 'CI') === FALSE && stripos($class, 'PEAR') === FALSE) {
foreach (array('flourish', 'mylibrary') as $folder){
if (is_file(APPPATH."../auxengines/{$folder}/{$class}.php")){
include_once APPPATH."../auxengines/{$folder}/{$class}.php";
}
}
}
}
spl_autoload_register('multi_auto_require');
Works brilliantly. Thanks, people!
来源:https://stackoverflow.com/questions/3823192/conflict-between-codeigniter-autoload-and-flourish-autoload-functions