3

php的一个类定义先后顺序的问题

 2 years ago
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.
neoserver,ios ssh client

php的一个类定义先后顺序的问题

Rivsen Tan |

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

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK