php的一个类定义先后顺序的问题
source link: https://rivsen.github.io/post/php-class-define-order-problem
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
php的一个类定义先后顺序的问题
blog and php |
2013-8-28
今天在玩 symfony 的 event-dispatcher 的时候遇到了个奇怪的问题: 我定义的类无法找到,它继承了Event,可是去掉继承语句却能正常调用。
<?php
namespace
{
require_once "vendor/autoload.php";
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\Event;
$a = new Test\a();
var_dump($a->getName());
}
namespace Test
{
use Symfony\Component\EventDispatcher\Event;
class a extends Event
{
public function getName()
{
return 'ger';
}
}
}
后来在qq群、irc上提问了,然后回复说我应该先定义这个namespace再使用,尤其是在单个文件中。
本来php平时用的时候是可以先调用后定义的,不过貌似php还没做到那么完整。于是我找到了一些有意思的博客,其中一个就是类的继承问题,其实这个类未定义和命名空间无关,而是和类的定义顺序有关,相关链接:点这里 。
<?php
class A extends B {} class B extends C {} class C {}
上边这段代码会提示类B未找到,但是把B和C的定义顺序调换,就可以正常运行了。
<?php
class A extends B {} class C {} class B extends C {}
上边的博客中给了个php.net上的链接,感兴趣的可以去看看: extends 。
Recommend
-
57
林小春周舟/新华社新华社华盛顿2月8日电,正当人们关注美国“猎鹰重型”火箭携带特斯拉跑车飞向火星时,两颗小行星在3天内先后与地球“擦肩而过”。但不用担心,它们不会对我们造成威胁。就在2月6日“猎鹰重型”发射前约半小时,小行星2018CC以1
-
37
@肖浑:朴树先后两次回应为什么不想生孩子。
-
36
-
27
钱七虎院士先后两次共向武汉捐款650万元
-
10
小心PHP的类定义顺序与继承的问题 2011-06-15 以下代码的运行环境均为PHP5.3.4 先来看一段代码 <?php class A extends B {} class B {} 代码很简单,也可以...
-
9
因疫情影响,苹果先后关闭洛杉矶地区和加州的零售店 据外媒 MacRumors 报道,由于加利福尼亚州新冠肺炎病例不断增加,在临时关闭洛杉矶地区的零售店后,苹果公...
-
8
英国公共卫生部:英国电子烟用户主要为烟民,非烟民占比不足3%。
-
9
关于微信小程序 wx.login 和 wx.getUserInfo 调用的先后顺序wx.getUserInfo({ success: res => { wx.login({ success: data => { wx.request({ url: 'https://...
-
2
如何正确定义测试阶段训练?顺序推理和域适应聚类方法 作者:机器之心 2022-11-10 15:58:41 华南理工、A*STAR 团队和鹏城实验室联合提出了针对测试阶段训练(TTT)问题的系统性分类准则。
-
3
V2EX › MySQL 请教一个 MYSQL 联合索引字段顺序的问题 ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK