[SOLVED] Class Loading failure in 3.x? Worked in 2.x
I'm writing a new class that integrates into the HOOK below. I'm getting a class loading error, so I'm assuming I'm doing something really silly wrong. It seems that the PHP function get_class_methods($strClass) is called and a NULL result is obtained. Also, I'm getting a "Cannot redeclare class", but I'm assuming this might be the error above first.
This all worked perfectly in 2.x, so this must be a 3.x thing. Anyone with ideas?
config/config.php
Code:
$GLOBALS['TL_HOOKS']['getUserNavigation'][] = array('Favorites', 'getFavorites');
classes/Favorites.php
Code:
/**
* Run in a custom namespace, so the class can be replaced
*/
namespace Contao;
/**
* Class Favorites
*
* Adds Favorite system to Contao
* @copyright Thyon Design 2013
* @author John Brand <http://www.thyon.com>
* @package Controller
*/
class Favorites extends \Backend
{
…
Code:
Warning: in_array() expects parameter 2 to be array, null given in /home/onlinbuy/public_html/system/modules/core/library/Contao/System.php on line 110
#0 [internal function]: __error(2, 'in_array() expe...', '/home/onlinbuy/...', 110, Array)
#1 /home/onlinbuy/public_html/system/modules/core/library/Contao/System.php(110): in_array('getInstance', NULL)
#2 /home/onlinbuy/public_html/system/modules/core/classes/BackendUser.php(475): Contao\System->import('Favorites')
#3 /home/onlinbuy/public_html/contao/main.php(275): Contao\BackendUser->navigation()
#4 /home/onlinbuy/public_html/contao/main.php(125): Main->output()
#5 /home/onlinbuy/public_html/contao/main.php(319): Main->run()
#6 {main}
Fatal error: Cannot redeclare class Contao\Favorites in /home/onlinbuy/public_html/system/modules/favorites/classes/Favorites.php on line 585
Re: Class Loading failure in 3.x? Worked in 2.x
Hi,
Have you create config/autoload.php ?
Best regards.
Re: Class Loading failure in 3.x? Worked in 2.x
Yes of course, otherwise it doesn't even find the class.
Re: Class Loading failure in 3.x? Worked in 2.x
Hello, again.
What about change your config/config.php:
Code:
$GLOBALS['TL_HOOKS']['getUserNavigation'][] = array('Contao\Favorites', 'getFavorites');
Best regards.
Re: Class Loading failure in 3.x? Worked in 2.x
Good idea. Now it says "Class 'Contao/Favorites' not found". I think it finds the class but perhaps it's loading twice or once loading it's not loading correctly.
Re: Class Loading failure in 3.x? Worked in 2.x
Aha, I mistook the target file. It wasn't config.php but autoload.php, for example:
Code:
ClassLoader::addClasses(array
(
'Contao\Favorites' => 'system/modules/favorites/classes/Favorites.php',
...
));
Best regards.
Re: Class Loading failure in 3.x? Worked in 2.x
Nope it's not that, because it it correctly rendered by Contao's autoloader.
Re: Class Loading failure in 3.x? Worked in 2.x
Re: Class Loading failure in 3.x? Worked in 2.x
Hey there everyone,
These loading issues happen, because the ClassLoader does not realize the class is already there and includes the file again. This usually happened to me when the class was loaded from another namespace, and the global alias was not used.
In your case, the class is in the "Contao" namespace, but the main.php script (which generated the navigation, triggers the hook and loads the class) is not in a namespace. I'd try the following, maybe that works:
Code:
$GLOBALS['TL_HOOKS']['getUserNavigation'][] = array('\Favorites', 'getFavorites');
Re: Class Loading failure in 3.x? Worked in 2.x
I found the problem. I hadn't yet added the namespace to the class when I ran the Autoloader. It created the class without the Contao\, so I had to add that manually, but unfortunately I added it using the "/" character instead of "\".
Oh dear, I knew this was a dufus alert! :oops:
Thanks to taca and andreas. This is probably a very important character check to highlight with developers -- I should just have re-rendered the autoloader files automatically, instead of editing them.