dongtao5104 2012-11-04 02: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 09: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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证