Results 1 to 10 of 10

Thread: [SOLVED] Class Loading failure in 3.x? Worked in 2.x

  1. #1
    Experienced user
    Join Date
    06-10-09.
    Location
    Cape Town, South Africa
    Posts
    1,387

    Default [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

  2. #2
    User taca's Avatar
    Join Date
    06-20-09.
    Location
    Kyoto, Japan
    Posts
    111

    Default Re: Class Loading failure in 3.x? Worked in 2.x

    Hi,

    Have you create config/autoload.php ?

    Best regards.
    --
    Takahiro Kambe

  3. #3
    Experienced user
    Join Date
    06-10-09.
    Location
    Cape Town, South Africa
    Posts
    1,387

    Default Re: Class Loading failure in 3.x? Worked in 2.x

    Yes of course, otherwise it doesn't even find the class.

  4. #4
    User taca's Avatar
    Join Date
    06-20-09.
    Location
    Kyoto, Japan
    Posts
    111

    Default 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.
    --
    Takahiro Kambe

  5. #5
    Experienced user
    Join Date
    06-10-09.
    Location
    Cape Town, South Africa
    Posts
    1,387

    Default 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.

  6. #6
    User taca's Avatar
    Join Date
    06-20-09.
    Location
    Kyoto, Japan
    Posts
    111

    Default 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.
    --
    Takahiro Kambe

  7. #7
    Experienced user
    Join Date
    06-10-09.
    Location
    Cape Town, South Africa
    Posts
    1,387

    Default 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.

  8. #8
    User taca's Avatar
    Join Date
    06-20-09.
    Location
    Kyoto, Japan
    Posts
    111

    Default Re: Class Loading failure in 3.x? Worked in 2.x

    Then, I have no idea. :?
    --
    Takahiro Kambe

  9. #9
    Official Contao Team andreas.schempp's Avatar
    Join Date
    06-10-09.
    Posts
    63

    Default 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');
    terminal42 gmbh
    We are Contao Premium-Partner! For Isotope eCommerce support check out the Isotope Circle.

  10. #10
    Experienced user
    Join Date
    06-10-09.
    Location
    Cape Town, South Africa
    Posts
    1,387

    Default 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! ops:

    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •