PHP properties with get/set functions

发布于 2022-09-06 11:01:51 字数 1816 浏览 18 评论 0

One of the handy features of other languages is the ability to create get and set methods for properties. In trying to find a good way to duplicate this functionality in PHP, I stumbled across this: http://www.php.net/manual/en/language.oop5.magic.php#98442

Here is my breakdown of that class:

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

}

?>

My plan was to extend this class and define the appropriate get_someproperty() and set_someproperty() in this extended class.

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

The trouble is, the base class of ObjectWithGetSetProperties is unable to see my method get_someproperty() in SomeNewClass. I always get the error, "key is not available".

Is there any way to resolve this, allowing the base class of ObjectWithGetSetProperties to work, or will I have to create those __get() and __set() magic methods in each class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不语却知心 2022-09-13 11:01:52

Try is_callable instead. Example code-fragment:

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

(Updated to show where B overrides A)

顾铮苏瑾 2022-09-13 11:01:52

That's not well documented (some mentions in the comments), but method_exists() really only checks for method presence in the current class.

But you can use is_callable() instead. It also verifies that the method not only exists, but is indeed allowed to be invoked:

 if (  is_callable(array($this, $varName))  ) {
     ...
毁我热情 2022-09-13 11:01:52

In your example, you'll need to declare the $_someproperty property, like this

class SomeNewClass extends ObjectWithGetSetProperties {
    protected $_someproperty;
    public function get_someproperty() {
        return $this->_someproperty;
    }
}
森罗 2022-09-13 11:01:52
<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文