Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在父方法中获取子类名称_Php_Oop - Fatal编程技术网

Php 在父方法中获取子类名称

Php 在父方法中获取子类名称,php,oop,Php,Oop,我在父类中有一个函数,我扩展了父类,现在我想在父函数中得到子类的名称并执行一些操作 下面是我正在尝试的代码示例,但这对我不起作用 父类(Foo) 子类(酒吧) 如何在父方法中获取子类名称Barself::class不返回子类。您可以使用后期静态绑定来执行此操作 后期静态绑定通过存储上次“非转发调用”中命名的类来工作 使用self::class而不是使用static::class class Foo { function method(){ $childeClass =

我在父类中有一个函数,我扩展了父类,现在我想在父函数中得到子类的名称并执行一些操作

下面是我正在尝试的代码示例,但这对我不起作用

父类(Foo)

子类(酒吧)


如何在父方法中获取子类名称Bar
self::class
不返回子类。

您可以使用后期静态绑定来执行此操作

后期静态绑定通过存储上次“非转发调用”中命名的类来工作

使用
self::class
而不是使用
static::class

 class Foo {
     function method(){
        $childeClass = static::class;
        // code
     }
 }
欲了解更多信息,请阅读

class Bar extends Foo{}
 class Foo {
     function method(){
        $childeClass = static::class;
        // code
     }
 }