We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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



58.4k

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


47.7k
edited May '15

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



5.7k
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 /



47.7k

Ah! Doh!

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



5.7k

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



47.7k
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.