duangu8264 2018-10-04 04:49
浏览 54

如何/可以在常量请求中定义常量?

Alright, here it goes. This is what I am currently using, it works fine, but I got hung up on this idea...

class Estr {

    public static function __callStatic ( $name, $arguments ) {
        $class_vars = get_class_vars ( get_called_class ( ) );
        if ( is_array ( $class_vars ) && sizeof ( $class_vars ) > 0 ) {
            foreach ( $class_vars as $key => $value ) {
                if ( strtolower ( $key ) == strtolower ( $name ) ) {
                    return $key;
                }
            }
        }
    }
}



class Relationship extends Estr {

    public $alternate;

}

This allows me to call;

var_dump ( Relationship :: alternate ( ) ); // response "alternate"

All is well and great. However, when I started I wanted the following result;

var_dump ( Relationship :: alternate ); // response "alternate"

And I want my relationship class to be either;

class Relationship extends Estr {

    public $alternate;

}

or

class Relationship extends Estr {

    const alternate;

}

Where class Estr is handling returning the variable/constants name as the value, with a clean and OOP layout. (eg; 'const alternate = "alternate";' and 'const alternate = null;' are not desired.)

I attempted a few solutions, the closest I got to achieving the goal was attempting to catch the error of calling the undefined constant, while still returning the value. However, I still could not achieve the class hierarchy I desired.

Thanks for the noodle, enjoy the puzzle! XD

P.S. Or better yet, how can I static call a non-existing class constant and handle the error within that class? Eg; Relationship :: alternate Response; "error within class Relationship"

Is there a way to intercept the call for a "defined constant," even though by nature they should exist? (If not, this may be answered here; PHP => Call to non-existing class constants, way to handle?)

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 DIFY API Endpoint 问题。
    • ¥20 sub地址DHCP问题
    • ¥15 delta降尺度计算的一些细节,有偿
    • ¥15 Arduino红外遥控代码有问题
    • ¥15 数值计算离散正交多项式
    • ¥30 数值计算均差系数编程
    • ¥15 redis-full-check比较 两个集群的数据出错
    • ¥15 Matlab编程问题
    • ¥15 训练的多模态特征融合模型准确度很低怎么办
    • ¥15 kylin启动报错log4j类冲突