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 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题