duanfen1312 2014-06-19 20:53
浏览 42

如何正确导入多个命名空间

I am very new to PHP namespaces and I have a question:

If we import multiple namespaces with the 'use' command, shouldn't we run into issues? I have been reading through this post http://www.sitepoint.com/php-namespaces-import-alias-resolution/, and in the section about Namespace Importing, it says that you still need to have a qualified prefix next to a function (ie: Lib2), but when looking at some Symfony 2 example files, I don't see that happening. How is namespacing solving (in the example file below) solving name conflicts?

namespace Acme\DemoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Acme\DemoBundle\Form\ContactType;

// these import the "@Route" and "@Template" annotations
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class DemoController extends Controller
{
/**
 * @Route("/", name="_demo")
 * @Template()
 */
public function indexAction()
{
    return array();
}

/**
 * @Route("/hello/{name}", name="_demo_hello")
 * @Template()
 */
public function helloAction($name)
{
    return array('name' => $name);
}

/**
 * @Route("/contact", name="_demo_contact")
 * @Template()
 */
public function contactAction(Request $request)
{
    $form = $this->createForm(new ContactType());
    $form->handleRequest($request);

    if ($form->isValid()) {
        $mailer = $this->get('mailer');

        // .. setup a message and send it
        // http://symfony.com/doc/current/cookbook/email.html

        $request->getSession()->getFlashBag()->set('notice', 'Message sent!');

        return new RedirectResponse($this->generateUrl('_demo'));
    }

    return array('form' => $form->createView());
}
}
  • 写回答

2条回答 默认 最新

  • douqiang7462 2014-06-19 22:06
    关注

    I think the misunderstanding comes from the post's filenames. keep in mind:

    • DefaultController.php with namespace Acme/AcmeBundle/Controller/ will be included with use Acme/AcmeBundle/Controller/DefaultController

    • DefaultController2.php with namespace Acme/AcmeBundle/Controller2/ will be included with use Acme/AcmeBundle/Controller2/DefaultController2

    To have a conflict you'd need to include two files named DefaultController.php

    • DefaultController.php with namespace Acme/AcmeBundle/Controller
    • DefaultController.php with namespace Acme/AcmeBundle/Controller2

    And would then need an alias as pointed out by Kevin

    • DefaultController.php with namespace Acme/AcmeBundle/Controller => use Acme/AcmeBundle/Controller/DefaultController
    • DefaultController.php with namespace Acme/AcmeBundle/Controller2 => use Acme/AcmeBundle/Controller2/DefaultController as DefaultController2
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统