Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

PHP Fatal error: Class not found in production environment - Autoloading behaviour different between dev and production

I have a class that is not loading when I use Phalcon 2/php5.5.9 that loads in phalcon 1.3.4/php5.5.22 .

Has loading behaviour on namespaces become more restrictive in Phalcon 2?

My model and all else loads but gets stuck not seeing my trait class .

bog standard loader:

$loader = new \Phalcon\Loader();

$loader->registerDirs(
    array(
        $config->application->controllersDir,
        $config->application->modelsDir,
        $config->application->libraryDir,
        $config->application->pluginsDir
    )
)->register();

app->models->User.php

class User extends \Phalcon\Mvc\Model
{
use Traits\MyTimestampable; //Error is here with - PHP Fatal error:  Trait 'Traits\\MyTimestampable' not found

}

app->models->traits->MyTimestampable.php

namespace Traits;

trait MyTimestampable 
{

    public function beforeValidationOnCreate()
    {
        $this->created = date("Y:m:d H:i:s");
        $this->modified = date("Y:m:d H:i:s");
    }

    public function beforeUpdate()
    {
        $this->modified = date("Y:m:d H:i:s");
    }

}

Any idea/hints etc. ?



31.7k

Because You use loadoer is register you should change to registerNamespaces

$loader->registerNamespaces(
    array(
        $config->application->controllersDir,
        $config->application->modelsDir,
        $config->application->libraryDir,
        $config->application->pluginsDir
    )
)->register();


20.2k
edited May '15

I don't use this in my local environment and yet it works. hm.



4.0k
Accepted
answer
edited May '15

It almost sounds like you have an additional autoloader that will search [included paths]/traits/MyTimestampable.php when you try to load \Traits\MyTimestampable

This should fix the namespace loader issue though

    $loader = new \Phalcon\Loader();

    $loader->registerDirs(
        array(
            $config->application->controllersDir,
            $config->application->modelsDir,
            $config->application->libraryDir,
            $config->application->pluginsDir
        )
    );

    $loader->registerNamespaces(
        array(
            'Traits' => $config->application->modelsDir . 'traits/'
        )
    );

    $loader->register();

That should work, keeping in mind that your config directories should always end with /



20.2k

Ah! Doh!

Thank you so much for the example. It worked a treat!



4.0k

You're welcome. I corrected a typo in my example. Accidentally has $loader->->register() and it should have been $loader->register();



20.2k
edited May '15

Tidy.

This is most unfortunate though as my dev environment is automatically autoloading and I don't seem to be able to track it down and turn it off. I have no idea where it is setup or where the functionality is coming from. Until both environments are contiguous with one another particularly on this side it could prove most frustrating.