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

namespace model in CLI

Hi I'm having hard time with namespace in models inside a CLI application :

    $loader = new \Phalcon\Loader();

    $loader->registerNamespaces(array(

        'Tuango\CLI\Task' => APPLICATION_PATH . '/tasks',
        'Tuango\CLI\Security' => APPLICATION_PATH . '/lib/Tuango/CLI/Security/',
        'Tuango\Models' => APPLICATION_PATH . '/models/Tuango/Models/'
    ));

    $loader->registerDirs(

        array(

            //APPLICATION_PATH . '/tasks',
            //APPLICATION_PATH . '/models'
        )
    );

    $loader->register();

Parent class :

namespace Tuango\CLI\Security;

abstract class Fraud  {
        //Abstract stuff here
}

Child class :

    namespace Tuango\CLI\Security;

    use Tuango\CLI\Security\Fraud;

    class LiveFraud extends Fraud {
        //Class stuff and abstract method inherited
    }

Task :

    namespace Tuango\CLI\Task;

    use Tuango\CLI\Security\LiveFraud;

    class FraudTask extends \Phalcon\CLI\Task {

        public function mainAction() {

            $liveFraud = new LiveFraud();
            $liveFraud->execute();
        }
    }
Fatal error: Class 'Tuango\CLI\Security\Fraud' not found in /Library/WebServer/Documents/tuangocli/app/lib/Tuango/CLI/Security/LiveFraud.php on line 30

I can't figure out what's the problem here.

Any help would be appreciated :)

Thank you.

First one in the list is missing a trailing slash, probably that won't fix your issue but just thought to mention it.

Anyways i'm using the composer autoloader, but if you're not using composer that wouldn't make much sense.



31.3k

Hi,

With or without the trailling slash doesn't make any difference. I just think there is something wrong with inheritance in this context

Thank you !

Other then the use statement being redundant i don't really see much wrong with this to be honest. It has the right namespace and loading it this way should just work. Try removing the use statement from LiveFraud, worth a shot. Also check if the file is there and has the correct filename. Could be something as stupid and simple as that.



31.3k
edited May '14

I can't remove the use statement. It has to be done, if not, what's the purpose of namespace if it's not to have nice distinction between to classes having the same name but a different meaning of use :

namespace Tuango\CLI\Security;

class Fraud {
}

namespace Tuango\Models\Fraud;

class Fraud {
}

Without namespace, how do I say to the interpreter wich one of Fraud models or factory class I'm using ... that's the point of namespacing in OOP. And I agree with you, can't see what's wrong since if I'm taking the same code, put it in a standard application and it works perfectly. I have the same structure in a Phalcon "Module" application. And it's working very nicely.

Namespace should be there, but it's using a class in the same namespace. Then the use statement is redundant. As in both LiveFraud and Fraud are in the same namespace.



31.3k
edited May '14

Event if I put the class in the same file, it's not working !!!! Ok now I'm very confuse about that and I start missing Zend ;)

class FraudFactory  {

    public function __construct() {

    }
}

class LiveFraud2 extends FraudFactory {
}

php cli.php fraud

Fatal error: Class 'FraudFactory' not found in /Library/WebServer/Documents/tuangocli/app/tasks/FraudTask.php on line 377

$loader = new \Phalcon\Loader();

$loader->registerDirs(

array(

    APPLICATION_PATH . '/tasks',
    APPLICATION_PATH . '/lib',
    APPLICATION_PATH . '/models'
));

$loader->register();


31.3k
edited May '14

This is how my $loader looks like before the register command :

Phalcon\Loader object {
    _eventsManager => null
    _foundPath => null
    _checkedPath => null
    _prefixes => null
    _classes => null
    _extensions => array(1) (
    [0] => (string) php
)
_namespaces => null
_directories => array(3) (
    [0] => (string) /Library/WebServer/Documents/tuangocli/app/tasks
    [1] => (string) /Library/WebServer/Documents/tuangocli/app/lib
    [2] => (string) /Library/WebServer/Documents/tuangocli/app/models
)
_registered => (bool) false
}

and after :

Phalcon\Loader object {
    _eventsManager => null
    _foundPath => null
    _checkedPath => null
    _prefixes => null
    _classes => null
    _extensions => array(1) (
    [0] => (string) php
)
_namespaces => null
_directories => array(3) (
    [0] => (string) /Library/WebServer/Documents/tuangocli/app/tasks
    [1] => (string) /Library/WebServer/Documents/tuangocli/app/lib
    [2] => (string) /Library/WebServer/Documents/tuangocli/app/models
)
_registered => (bool) true
}

It's properly registered.