简体   繁体   中英

How can i access a Model in another Namespace in Zend2?

i am new to zend2, i have created a model(class) in a namespace, and i want to make an instance of this model within a controller from another namespace , so do you know how can i do this?

Thanks Tarek

Please put classes names and namespaces here. Of course, you can create class from another namespace, all you need is to use that namespace. Eg

   $x = new MyNamespace\SuperClass\Hello\MySuperClass();
   ----     <---------namespace---------><-className---> 

to avoid writing each time whole namespace, you can use use at beginning of the script

namespace currentNamespace;

use MyNamespace\SuperClass\Hello;

class myClass() {
    public function something() {
        $x = new MySuperClass(); // instead of MyNamespace\SuperClass\Hello\MySuperClass();
    }
}

I found a solution as following: in Module.php i should register my Model(class):

public function getServiceConfig()
{
    return array(

        'invokables' => array(
            'User' => 'Application\Model\User'
        ),            
    );
}

and later when i want to use it within another class i call it this way:

$User = $this->getServiceLocator()->get('User');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM