Php 你能扩展别名吗?如果可以,怎么扩展?

Php 你能扩展别名吗?如果可以,怎么扩展?,php,namespaces,Php,Namespaces,我对名称空间没有太多经验,我唯一的例子就是使用Laravel <?php use My\Full\Classname as Another; // <-- Alias // Want to add in import here to extend Faker alias to include more methods 我想能够扩展另一个,这样我就可以做另一个->foo();别名可以这样做吗?只需创建一个新类即可 class ExtendedAnother extends Anot

我对名称空间没有太多经验,我唯一的例子就是使用Laravel

<?php

use My\Full\Classname as Another; // <-- Alias
// Want to add in import here to extend Faker alias to include more methods

我想能够扩展另一个,这样我就可以做另一个->foo();别名可以这样做吗?

只需创建一个新类即可

class ExtendedAnother extends Another {
    function foo() {
        $this->bar();
        // Do foo here
    }
}
现在,您只能执行以下操作

use ExtendedAnother as Another; // Has foo() method
Another->foo();

别名或名称空间不是用于类扩展的,因此不能以您请求的方式使用。

默认情况下,您应该能够调用
$faker->句子()
。@prd由于某种原因抛出了错误。我删除了句子()方法并运行了我的播种机。我只是回去把它加上去,它就起作用了。奇怪的我会改变我的例子,因为这仍然是我好奇的事情。
use ExtendedAnother as Another; // Has foo() method
Another->foo();