简体   繁体   中英

How can I get name of interface from abstract class in PHP

It is possible in php to get an interface name without creating a new object?? For example:

interface my_interface{}

abstract class foo implements my_interface{

      public static function get_interface(){
             // here return name of interface
      }

}

class bar extends foo{}

echo bar::get_interface();

Expected Output :

 my_interface

Make use of the Reflection Class

$rc1 = new ReflectionClass("foo");
echo $rc1->getInterfaceNames()[0];

The code..

<?php
interface my_interface{}

abstract class foo implements my_interface{

    public static function get_interface(){
        // here return name of interface
    }

}

$rc1 = new ReflectionClass("foo");
echo $rc1->getInterfaceNames()[0];

OUTPUT :

my_interface
$r = new ReflectionClass(get_class()); // or get_called_class
foreach ($r->getInterfaceNames() as $name) {
    echo $name;
}

See http://php.net/manual/en/reflectionclass.getinterfacenames.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM