带有get / set函数的PHP属性

时间:2011-02-07 16:58:56

标签: php oop inheritance magic-methods

其他语言的一个便利功能是能够为属性创建get和set方法。在试图找到一种在PHP中复制此功能的好方法时,我偶然发现了这一点:http://www.php.net/manual/en/language.oop5.magic.php#98442

以下是我对该课程的分类:

<?php

class ObjectWithGetSetProperties {

    public function __get($varName) {
        if (method_exists($this,$MethodName='get_'.$varName)) {
            return $this->$MethodName();
        } else {
            trigger_error($varName.' is not avaliable .',E_USER_ERROR);
        }
    }

    public function __set($varName,$value) {
        if (method_exists($this,$MethodName='set_'.$varName)) {
            return $this->$MethodName($value);
        } else {
            trigger_error($varName.' is not avaliable .',E_USER_ERROR);
        }
    }

}

?>

我的计划是扩展此类,并在此扩展类中定义适当的get_someproperty()set_someproperty()

<?php
class SomeNewClass extends ObjectWithGetSetProperties {
    protected $_someproperty;
    public function get_someproperty() {
        return $this->_someproperty;
    }
}
?>

问题是,ObjectWithGetSetProperties的基类无法在get_someproperty()中看到我的方法SomeNewClass。我总是得到错误,“密钥不可用”。

有没有办法解决这个问题,允许ObjectWithGetSetProperties的基类工作,还是我必须在每个类中创建那些__get()__set()魔术方法?

4 个答案:

答案 0 :(得分:5)

请尝试使用is_callable。示例代码片段:

<?php
date_default_timezone_set("America/Edmonton");
class A {
    protected $_two="goodbye";
    protected $_three="bye";
    protected $_four="adios";
    public function __get($name) {
        if (is_callable(array($this,$m="get_$name"))) {
            return $this->$m();
        }
        trigger_error("Doh $name not found.");
    }
    public function get_two() {
        return $this->_two;
    }
}
class B extends A {
    protected $_one="hello";
    protected $_two="hi";
    protected $_three="hola";
    public function get_one() {
        return $this->_one;
    }
    public function get_two() {
        return $this->_two;
    }
    public function get_three() {
        return $this->_three;
    }
    public function get_four() {
        return $this->_four;
    }
}

$a=new a();
echo $a->one."<br />";//Doh one not found.
echo $a->two."<br />";//goodbye
echo $a->three."<br />";//Doh three not found.
echo $a->four."<br />";//Doh four not found.
$b=new b();
echo $b->one."<br />";//hello
echo $b->two."<br />";//hi
echo $b->three."<br />";//hola
echo $b->four."<br />";//adios
?>

(已更新以显示B覆盖A

的位置

答案 1 :(得分:3)

没有详细记录(comments中有一些提及),但method_exists()实际上只检查当前类中的方法是否存在。

但您可以使用is_callable()代替。它还验证该方法不仅存在,而且确实允许调用:

 if (  is_callable(array($this, $varName))  ) {
     ...

答案 2 :(得分:0)

在您的示例中,您需要声明$_someproperty属性,如此

class SomeNewClass extends ObjectWithGetSetProperties {
    protected $_someproperty;
    public function get_someproperty() {
        return $this->_someproperty;
    }
}

答案 3 :(得分:0)

<form  id="fname" method="post">
    <textarea name="sms" id="messageText"></textarea>
    <input type="submit" name="smsbut"  value="sms"/>
</form> 
<script>
    $(document).ready(function() {
        $("#fname").submit(function()
        {
            var sms = $('#messageText').val();
            $.ajax(
                    {
                        type: "POST",
                        url: "sms.php/?id=<?php echo $_SESSION['id']; ?>",
                        data: {userSms: sms},
                        dataType: "json",
                        success: function(result)
                        {
                            if (result["error"] != undefined)
                            {

                            }
                            else if (result["success"] != undefined)
                            {
                                $('#messageText').val("");
                                location.reload();
                            }
                        }
                    });
            return false;
        });
    });
</script>