gpt4 book ai didi

php - 如何从一个类中获取常量,排除所有可能从 parent 那里继承下来的常量?

转载 作者:可可西里 更新时间:2023-11-01 12:35:08 25 4
gpt4 key购买 nike

在第二个示例中,PDO 正在用它的常量填充 MyClass,如何过滤掉它们?

class MyClass {
const PARAM_1 = 1;
const PARAM_2 = 2;
const PARAM_3 = 4;

function MyMethod() {
$reflector = new ReflectionClass(__CLASS__);
print_r($reflector->getConstants());
}

}

$myInstance = new MyClass();
$myInstance->MyMethod();

//returns:
//Array
//(
// [PARAM_1] => 1
// [PARAM_2] => 2
// [PARAM_3] => 4
//)

class MyClassPDO extends PDO {
const PARAM_1 = 1;
const PARAM_2 = 2;
const PARAM_3 = 4;

function MyMethod() {
$reflector = new ReflectionClass(__CLASS__);
print_r($reflector->getConstants());
}

}

$myInstancePDO = new MyClassPDO('sqlite::memory:');
$myInstancePDO->MyMethod();

//Array
//(
// [PARAM_1] => 1
// [PARAM_2] => 2
// [PARAM_3] => 4
// [PARAM_BOOL] => 5
// [PARAM_NULL] => 0
// [PARAM_INT] => 1
// [PARAM_STR] => 2
// [PARAM_LOB] => 3
// [PARAM_STMT] => 4
// [PARAM_INPUT_OUTPUT] => -2147483648
// [PARAM_EVT_ALLOC] => 0
// [PARAM_EVT_FREE] => 1
// [PARAM_EVT_EXEC_PRE] => 2
//....and so on

最佳答案

据我所知

function MyMethod() {
$reflector = new ReflectionClass(__CLASS__);
print_r(array_diff($reflector->getConstants(),$reflector->getParentClass()->getConstants()));
}

关于php - 如何从一个类中获取常量,排除所有可能从 parent 那里继承下来的常量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6597124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com