[英]PHP Opcode Caching/Zend Acceleration and include_once vs. require_once
[英]symfony 'use' vs. include_once
我刚刚开始使用symfony(2),并且尝试从现有类中移植一些代码-但尝试在symfony框架中使用“旧”范例时,我有点卡住了。 我有以下代码:
namespace AppBundle\Controller;
use AppBundle\Components\model\Customer;
class MyController extends Controller
{
/**
* @Route("/index.php", name="index")
*/
public function indexAction(Request $request)
{
include_once( 'AppBundle\Components\model\Customer.php' );
$entity = new Customer();
$this->get('logger')->info('Instance == > ' . isset($entity) );
$this->get('logger')->info('Class exists == > ' . ( class_exists('Customer') ? 'YES' : 'NO') );
其中Customer.php仅包含一个称为Customer的空类。
但是,此代码始终记录:
[2016-06-24 17:44:42] app.INFO: Instance == > 1 [] []
[2016-06-24 17:44:42] app.INFO: Class exists == > NO [] []
如您所见,我同时use
和include_once
。 但是似乎我可以创建一个实例,但是class_exists
返回false。
在我看来,这是一个矛盾。 有人可以告诉我如何设置它,以便class_exists
返回true。
use
仅建立别名,因此您可以使用较短的名称来引用类名称。 完全合格的类名称为AppBundle\\Components\\model\\Customer
; use
语句允许您仅使用Customer
来引用它。
但是 ,在使用字符串引用类时,始终需要使用完全限定的类名。 use
建立的别名不适用于字符串,因为这些别名不受use
语句的词法范围的约束,因此无法可靠地对其进行解析。
这有效:
class_exists('AppBundle\Components\model\Customer')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.