1. 程式人生 > >返回當前類所有常量的Key=>value 集合

返回當前類所有常量的Key=>value 集合

常量 clas dump stat array true function 所有 func

<?php

class Test
{
    const A = ‘1‘;
    const B = ‘2‘;
    const C = ‘3‘;
    const D = ‘4‘;
    const E = ‘5‘;
    const F = ‘6‘;

    public static function all()
    {
        $static = new static();
        $class = new \ReflectionClass($static);
        $constants = $class->getConstants();
        return $constants;
    }
}

var_dump(Test::all());
//打印結果如下:
//array (size=6)
//  ‘A‘ => string ‘1‘ (length=1)
//  ‘B‘ => string ‘2‘ (length=1)
//  ‘C‘ => string ‘3‘ (length=1)
//  ‘D‘ => string ‘4‘ (length=1)
//  ‘E‘ => string ‘5‘ (length=1)
//  ‘F‘ => string ‘6‘ (length=1)

  

返回當前類所有常量的Key=>value 集合