1

其他语言的便利功能之一是能够为属性创建 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

4 回答 4

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

于 2011-02-07T17:10:03.340 回答
3

这没有很好的记录(在评论中提到了一些),但method_exists()实际上只检查当前类中是否存在方法。

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

 if (  is_callable(array($this, $varName))  ) {
     ...
于 2011-02-07T17:10:43.763 回答
0

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

class SomeNewClass extends ObjectWithGetSetProperties {
    protected $_someproperty;
    public function get_someproperty() {
        return $this->_someproperty;
    }
}
于 2011-02-07T17:10:30.860 回答
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>
于 2013-09-23T11:02:13.077 回答