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

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算