I've created my code as ZF2 pushes you to do it and now I'm starting to think that it's actually against the whole point/benefit of using namespacing in the first place.
I want to change everything but I'm scared to do it my way just because ZF didn't do it this way themselves so I feel like I have to be missing one important thing.
My folder/file structure is something like this:
- Application
- Controller
IndexController.php
- Model
- Table
User.php
Building.php
- Mapper
User.php
Building.php
- Entity
User.php
Building.php
So inside my controller, the code might look something like this, as ZF2 suggests you start out:
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController,
Zend\View\Model\ViewModel;
use Application\Model\Entity\User as UserEntity,
Application\Model\Mapper\User as UserMapper,
Application\Model\Table\User as UserTable;
class IndexController extends AbstractActionController {
public function indexAction() {
$userEntity = new UserEntity;
$userMapper = new UserMapper;
$userTable = new UserTable;
Right there I've only given a few items but as your application grows, you end up with a HUGE use statement and it seems like it should be done more like the following:
namespace Application;
use Zend\Mvc\Controller\AbstractActionController,
Zend\View\Model\ViewModel;
use Model;
class IndexController extends AbstractActionController {
public function indexAction() {
$userEntity = new Entity\User;
$userMapper = new Mapper\User;
$userTable = new Table\User;
I'm guessing it's because ZF2 are pushing Modular based projects that have lots of modules. But surely then I could just throw in that module's namespace if required? I would still need to do that with the more qualified names as currently in use.
The use
statement is to import a namespace and about how & when you import, there is no coding standard.
There are two things you have to keep in mind:
By the way, there is a use
PSR-2 guideline that states use statements must end with ;
for every single use. So not this:
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController,
Zend\View\Model\ViewModel;
use Application\Model\Entity\User as UserEntity,
Application\Model\Mapper\User as UserMapper,
Application\Model\Table\User as UserTable;
class IndexController extends AbstractActionController {
public function indexAction() {
$userEntity = new UserEntity;
$userMapper = new UserMapper;
$userTable = new UserTable;
}
}
But this:
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Model\Entity\User as UserEntity;
use Application\Model\Mapper\User as UserMapper;
use Application\Model\Table\User as UserTable;
class IndexController extends AbstractActionController {
public function indexAction() {
$userEntity = new UserEntity;
$userMapper = new UserMapper;
$userTable = new UserTable;
}
}
So you might clean things up to this:
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Model;
class IndexController extends AbstractActionController {
public function indexAction() {
$userEntity = new Model\Entity\User;
$userMapper = new Model\Mapper\User;
$userTable = new Model\Table\User;
}
}
And if you want to use your own entity and mapper, but a table from a different module, the code will get refactored into this:
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Model;
use OtherModule\Model\Table\User as UserTable;
class IndexController extends AbstractActionController {
public function indexAction() {
$userEntity = new Model\Entity\User;
$userMapper = new Model\Mapper\User;
$userTable = new UserTable;
}
}