【发布时间】:2014-06-19 20:53:42
【问题描述】:
我对 PHP 命名空间非常陌生,我有一个问题:
如果我们使用“use”命令导入多个命名空间,我们不应该遇到问题吗? 我一直在阅读这篇文章http://www.sitepoint.com/php-namespaces-import-alias-resolution/,在关于命名空间导入的部分中,它说您仍然需要在函数旁边有一个合格的前缀(即:Lib2),但是在查看一些 Symfony 2 示例文件时,我不认为会发生这种情况。 命名空间如何解决(在下面的示例文件中)解决名称冲突?
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());
}
}
【问题讨论】:
-
只有在发生冲突时才需要别名。许多用途不是问题。如果发生冲突,只需添加别名即可。
标签: php symfony namespaces