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

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?)

展开翻译

译文

好的,就在这里。 这是我目前正在使用的,它工作正常,但我对这个想法感到兴奋... </ p>

  class Estr {

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

class关系扩展Estr {

public $ alternate;

}
</ code> </ pre>

这允许我调用; </ p>

var_dump(Relationship :: alternate()); //响应“alternate”
</ code> </ pre>

一切都很好。 但是,当我开始时我想要以下结果; </ p>

  var_dump(Relationship :: alternate);  //响应“alternate”
</ code> </ pre>

</ strong>我希望我的关系类成为; </ p>

 类关系扩展Estr {

public $ alternate;

}
</ code> </ pre>

或</ em> </ p >

 类关系扩展Estr {

const alternate;

}
</ code> </ pre>

Estr类处理的位置 使用clean和OOP布局返回变量/常量名称作为值。 (例如;'const alternate =“alternate”;'和'const alternate = null;'是不可取的。)</ p>

我尝试了几个解决方案,我最接近实现目标 试图捕获调用未定义常量的错误,同时仍然返回值。 但是,我仍然无法实现我想要的类层次结构。</ p>

感谢面条,享受拼图! XD </ p>

P.S.
或者更好的是,如何静态调用不存在的类常量并处理该类中的错误?
Eg; Relationship :: alternate
Response; “类关系中的错误”</ p>

有没有办法拦截对“已定义常量”的调用,即使它本质上应该存在? (如果没有,可以在这里回答; PHP = &gt;调用不存在的类常量,处理方式?)</ p>
</ div>

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问