Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8789561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:26:33+00:00 2026-06-13T22:26:33+00:00

I’ve created my code as ZF2 pushes you to do it and now I’m

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:26:34+00:00Added an answer on June 13, 2026 at 10:26 pm

    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;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.