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_Laravel - Fatal编程技术网

Php 列出所有扩展类及其设置

Php 列出所有扩展类及其设置,php,oop,laravel,Php,Oop,Laravel,我有一个系统,我在其中创建多个类,这些类都是从一个抽象类扩展而来的 每个类还声明该特定类类型的“设置” 例如: class First extends Base { protected $name = 'First'; protected $lug = 'first'; protected $fields = [ 'name', 'address', 'phone', ]; function __cons

我有一个系统,我在其中创建多个类,这些类都是从一个抽象类扩展而来的

每个类还声明该特定类类型的“设置”

例如:

class First extends Base {

    protected $name = 'First';
    protected $lug = 'first';
    protected $fields = [
        'name',
        'address',
        'phone',
    ];

    function __construct()
    {
        parent::__construct();
    }

    public function abstractMethod()
    {
        // do stuff for this particular class
    }


}

现在,我希望能够抓取所有扩展类及其“设置”,并返回如下内容:

$classes = [
    'first' => [
        'name' => 'First',
        'slug' => 'first',
        'fields' => ['name', 'address', 'phone']
    ],
    'second' => [
        'name' => 'Second',
        'slug' => 'second-one',
        'fields' => ['first-name', 'last-name', 'email']
    ]
];
$result = array();
foreach (get_declared_classes() as $class) {
    if (is_subclass_of($class, 'Base')) {
        $obj = new $class;
        $refObj = new ReflectionObject($obj);
        $props = $refObj->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED);
        $classProps = array();
        foreach ($props as $prop) {
            $property = $refObj->getProperty($prop->getName());
            $property->setAccessible(true);
            $classProps[$prop->getName()] = $property->getValue($obj);
        }
        $result[$class] = $classProps;
    }
}
print_r($result);
那我该怎么做呢?有更好的办法吗

如果有帮助的话,我正在使用Laravel

编辑:解释为什么不复制


我不仅仅是想找到一种获取类及其信息的方法,我还想找到一种构建这种情况的方法。我本质上是在创建一个可扩展插件系统,需要一种方法来告诉不要问添加了哪些插件

我没有试过,但它应该能起作用。否则它会指引你

$result = array();
foreach (get_declared_classes() as $class) {
    if (is_subclass_of($class, 'Base'))
        $result[] = get_class_vars($class);
}
但是您的属性也需要是公共的。

如何使用?获取属性非常容易,下面的手册就是一个例子。列出扩展类也应该很容易

<?php
class Bar {
    protected $inheritedProperty = 'inheritedDefault';
}

class Foo extends Bar {
    public $property = 'propertyDefault';
    private $privateProperty = 'privatePropertyDefault';
    public static $staticProperty = 'staticProperty';
    public $defaultlessProperty;
}

$reflectionClass = new ReflectionClass('Foo');
var_dump($reflectionClass->getDefaultProperties());
使用以下方法,您可以这样做:

$classes = [
    'first' => [
        'name' => 'First',
        'slug' => 'first',
        'fields' => ['name', 'address', 'phone']
    ],
    'second' => [
        'name' => 'Second',
        'slug' => 'second-one',
        'fields' => ['first-name', 'last-name', 'email']
    ]
];
$result = array();
foreach (get_declared_classes() as $class) {
    if (is_subclass_of($class, 'Base')) {
        $obj = new $class;
        $refObj = new ReflectionObject($obj);
        $props = $refObj->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED);
        $classProps = array();
        foreach ($props as $prop) {
            $property = $refObj->getProperty($prop->getName());
            $property->setAccessible(true);
            $classProps[$prop->getName()] = $property->getValue($obj);
        }
        $result[$class] = $classProps;
    }
}
print_r($result);
输出:

array(5) {
   ["staticProperty"]=>
   string(14) "staticProperty"
   ["property"]=>
   string(15) "propertyDefault"
   ["privateProperty"]=>
   string(22) "privatePropertyDefault"
   ["defaultlessProperty"]=>
   NULL
   ["inheritedProperty"]=>
   string(16) "inheritedDefault"
}
Array (
    [First] => Array (
        [name] => First
        [lug] => first
        [fields] => Array (
             [0] => name
             [1] => address
             [2] => phone
         )
     )
     [Second] => Array (
         [name] => Second
         [lug] => second-one
         [fields] => Array (
             [0] => first-name
             [1] => last-name
             [2] => email
         )
     )
)

可能的副本很好而且简洁,我并不是有意要保护这些字段!是的,ReflectionClass会很好,所以我不必像ReflectionObject那样实例化它们。