__set 和字符串连接

发布于 2024-09-18 14:31:01 字数 984 浏览 4 评论 0原文

我想知道这是否可能:

我已经成功使用 __set() 魔术方法将值设置为类的属性:

class View
{
    private $data;

    public function __set( $key, $value )
    {
         $this->data[$key] = $value;
    }
}

所以我能够:

$view = new View();
$view->whatever = 1234;

例如,当我想连接字符串时,问题就出现了。看起来 __set() 没有被调用(事实上它没有被调用)。

$view = new View();
$view->a_string = 'hello everybody';    //Value is set correctly

$view->a_string.= '<br>Bye bye!';       //Nothing happens...

echo $view->a_string;

这输出“大家好”。我无法在第二个作业中执行 __set() 。 阅读 php.net 它说:

__set() is run when writing data to inaccessible properties.

所以由于 a_string 已经存在,所以不会调用 __set 。 我的问题最后是......我怎样才能实现串联操作?

注意: 好吧...我一发布这篇文章,墨菲就来给我答案...

答案(据我所知)是 PHP 无法像我一样决定 a_string 是否可用没有定义 __get() 方法。

定义 __get() 允许 php 查找 a_string 的当前值,然后使用 __set() 连接该值。

Im' wondering if this is possible:

I've successfully used __set() magic method to set values to properties of a class:

class View
{
    private $data;

    public function __set( $key, $value )
    {
         $this->data[$key] = $value;
    }
}

So I'm able to:

$view = new View();
$view->whatever = 1234;

The problem comes when I want to concatenate a string for example. It seems like __set() is not being called (it's not being called in fact).

$view = new View();
$view->a_string = 'hello everybody';    //Value is set correctly

$view->a_string.= '<br>Bye bye!';       //Nothing happens...

echo $view->a_string;

This outputs "hello everybody". I'm not able to execute __set() in the second assignment.
Reading php.net it says that:

__set() is run when writing data to inaccessible properties.

So as a_string already exists, __set is not called.
My question finally is... how could I achieve that concatenation operation??

Note:
Ok... Murphy came and gave me the answer as soon as I posted this...

The answer (As I understood), is that PHP is not able to decide if a_string is available as I didn't defined a __get() method.

Defining __get() allows php to find the current value of a_string, then uses __set() to concatenate the value.

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

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

发布评论

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

评论(3

乜一 2024-09-25 14:31:01

您应该添加一个 __get() 方法,该方法允许您访问不可访问的属性,就像使用 __set() 所做的那样。然后您可以执行以下操作:

$view->myvar = $view->myvar.'Added text.';

__get() 方法将是:

public function __get($var) {
  return (isset($this->data[$var])) ? $this->data[$var]: '';
}

You should add a __get() method that allows you to access inaccessable properties just as you did with __set(). You could then do the following:

$view->myvar = $view->myvar.'Added text.';

The __get() method would be:

public function __get($var) {
  return (isset($this->data[$var])) ? $this->data[$var]: '';
}
我们只是彼此的过ke 2024-09-25 14:31:01

仅供参考,对于简单的分配,魔术方法甚至不是必需的。

class A {

}

$a = new A();

$a->str = '1';
$a->str .= '2';
echo $a->str;  // outputs 12

这将工作得很好,因为 PHP 将通过赋值创建新属性。当需要对值运行额外的检查/代码时,通常使用 __set/get。

FYI, for simple assignment, magic methods aren't even necessary.

class A {

}

$a = new A();

$a->str = '1';
$a->str .= '2';
echo $a->str;  // outputs 12

This will work just fine as PHP will create the new properties through assignment. __set/get usually is used when additional checks/code need to be run on the values.

救星 2024-09-25 14:31:01

这是因为 __set() 是一个方法而不是字符串。如果您希望它像字符串一样“起作用”,您可以将其更改为这样。

class View
{
    private $data;

    public function __set( $key, $value )
    {
        if (empty($this->data[$key])) {
            $this->data[$key] = $value;
        } else {
            $this->data[$key] .= $value;
        }
    }
}

It's because __set() is a method and not a string. If you want it to "act" like a string you can change it to this.

class View
{
    private $data;

    public function __set( $key, $value )
    {
        if (empty($this->data[$key])) {
            $this->data[$key] = $value;
        } else {
            $this->data[$key] .= $value;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文