V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
haython
V2EX  ›  PHP

PHP 怎么限制一个函数或者方法的某参数必须是一个类的静态属性?

  •  
  •   haython · 2014-07-03 18:08:03 +08:00 · 3301 次点击
    这是一个创建于 3591 天前的主题,其中的信息可能已经有所发展或是发生改变。
    例如:

    class config
    {
    public static $message =1;
    }

    function a($b){}

    a(config::$message);


    我如果能够使调用a函数时,传入的参数必须是config里的一个属性?
    18 条回复    2014-07-03 21:51:20 +08:00
    zaishanfeng2014
        1
    zaishanfeng2014  
       2014-07-03 18:10:34 +08:00
    反射,可以吗?
    foccy
        2
    foccy  
       2014-07-03 18:14:50 +08:00
    有这样的需求,我觉得你应该在函数内检查。
    likexian
        3
    likexian  
       2014-07-03 18:17:08 +08:00
    $a = new ReflectionProperty('config', 'message');
    var_dump($a->isStatic());

    请叫我PHP大神然后点赞
    likexian
        4
    likexian  
       2014-07-03 18:21:09 +08:00
    好吧,我看错了,无视我吧
    haython
        5
    haython  
    OP
       2014-07-03 18:23:15 +08:00
    @likexian a(config::$message); 这样我在a函数内获取到的值是1
    likexian
        6
    likexian  
       2014-07-03 18:28:23 +08:00   ❤️ 1
    你往a传入的是个数值了,数值就是数值,他来自哪里是没有标记的,反射是反射不了了
    所以,你的需求基本上无解
    wesley
        7
    wesley  
       2014-07-03 18:28:30 +08:00
    function a($b){
    return property_exists(config,$b);
    }
    haython
        9
    haython  
    OP
       2014-07-03 18:32:25 +08:00
    @minbaby 可能你没明白我在说什么
    foccy
        10
    foccy  
       2014-07-03 18:33:29 +08:00
    这样?
    public function a($b)
    {
    $hit = false;
    $reflection = new ReflectionObject($this);
    foreach ($reflection->getProperties() as $property) {
    if ($property->isStatic()) { // 限制静态属性
    if ($b === $property->getValue()) {
    $hit = true;
    }
    }
    }
    if (!$hit) {
    throw new Exception('参数不在对象属性中');
    }
    }
    haython
        11
    haython  
    OP
       2014-07-03 18:36:06 +08:00
    @foccy 如果属性的值为1 ,那我直接传入1你这样判断不出来
    minbaby
        12
    minbaby  
       2014-07-03 18:36:52 +08:00   ❤️ 1
    @haython function 中你得到的参数是值,
    而你想要的是验证参数是 config 类的属性,
    那么肯定不能直接直接根据参数来判断,
    非要实现这个东西的话,我觉得只能改写方法,
    function a($class, $property){}
    类似,才能判断,
    wesley
        13
    wesley  
       2014-07-03 18:37:35 +08:00
    $a = new ReflectionClass('config');
    print_r( $a->getStaticProperties() );
    foccy
        14
    foccy  
       2014-07-03 18:41:32 +08:00
    @haython 那是我理解错你的需求了,这样目前来说我还不清楚。
    dorentus
        15
    dorentus  
       2014-07-03 20:33:38 +08:00   ❤️ 1
    你这背后的真实需求是什么?
    Actrace
        16
    Actrace  
       2014-07-03 21:02:19 +08:00
    <?php
    class type_a{
    public $val = 1;
    }

    function main(type_a $object){
    var_dump($object);
    }

    $new_type_a = new type_a;
    main($new_type_a);
    //结果会是
    //object(type_a)#1 (1) {
    // ["val"]=>
    // int(1)
    //}

    //如果你给的参数不是type_a类产生的对象.

    $new_object = 1;//实际上 int 是一种内置类型
    main($new_object);
    ////这将会报错
    //Catchable fatal error: Argument 1 passed to main() must be an instance of type_a, integer given
    Actrace
        17
    Actrace  
       2014-07-03 21:05:35 +08:00
    可以要求参数为具体类(string,int,function都是内置的类),不过没有尝试过具体到属性.你可以在实现的内部指定目标属性,需要自己检查.
    根据上面的帖子,稍微改下.
    function main(type_a $object){
    isset($object->val);
    }
    SoloCompany
        18
    SoloCompany  
       2014-07-03 21:51:20 +08:00   ❤️ 1
    如果你的真实需求是 enum,那么请使用真正的 enum
    另外,这种典型的 ab 问题还是少出显得好
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2106 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 04:35 · PVG 12:35 · LAX 21:35 · JFK 00:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.