dongtao5104 2012-11-03 18:52
浏览 45
已采纳

ZF2 / PHP命名空间 - 哪个更快/更好?

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.

展开全部

  • 写回答

1条回答 默认 最新

  • donglun2010 2012-11-04 01:15
    关注

    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:

    1. Yes, the use of many classes (or namespaces) might get too complicated. Therefore I usually import a namespace, without having the classes imported explicitly. Examples are here (for module features), here (for autoloading) and here (for exceptions). As you see, it's a mixture of FQCN and namespaces.
    2. If you are scared for "if your application grows": think about classes as they encapsulate responsibility. If your class has more than one responsibility, split it into two classes. Then you will also notice your number of imported namespaces will decrease.

    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;
        }
    }
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部