【问题标题】:PHP __set magic method not being called未调用 PHP __set 魔术方法
【发布时间】:2015-11-07 02:07:06
【问题描述】:

我目前正在制作一个基于对象的 API。我有一个名为Part 的抽象类,每个孩子都扩展它。 Part 有一个 __set 函数,它将值存储在一个名为 $attributes 的受保护数组中。但是,当我执行 $part->user = new User(etc...); 时,它不会运行 __set 函数。这是我的代码:

部分:

<?php

namespace Discord;

abstract class Part
{
    protected $attributes = [];

    public function __construct(array $attributes)
    {
        $this->attributes = $attributes;

        if (is_callable([$this, 'afterConstruct'])) {
            call_user_func([$this, 'afterConstruct']);
        }
    }

    /**
     * Handles dynamic get calls onto the object.
     * 
     * @param  string $name 
     * @return mixed
     */
    public function __get($name)
    {
        $str = '';

        foreach (explode('_', $name) as $part) {
            $str .= ucfirst($name);
        }

        $funcName = "get{$str}Attribute";

        if (is_callable([$this, $funcName])) {
            return call_user_func([$this, $funcName]);
        }

        if (!isset($this->attributes[$name]) && is_callable([$this, 'extraGet'])) {
            return $this->extraGet($name);
        }

        return $this->attributes[$name];
    }

    /**
     * Handles dynamic set calls onto the object.
     *
     * @param string $name 
     * @param mixed $value 
     */
    public function __set($name, $value)
    {
        echo "name: {$name}, value: {$value}";
        $this->attributes[$name] = $value;
    }
}

客户:

<?php

namespace Discord\Parts;

use Discord\Part;
use Discord\Parts\User;

class Client extends Part
{
    /**
     * Handles extra construction.
     * 
     * @return void
     */
    public function afterConstruct()
    {
        $request = json_decode($this->guzzle->get("users/{$this->id}")->getBody());

        $this->user = new User([
            'id'        => $request->id,
            'username'  => $request->username,
            'avatar'    => $request->avatar,
            'guzzle'    => $this->guzzle
        ]);
    }

    /**
     * Handles dynamic calls to the class.
     *
     * @return mixed 
     */
    public function __call($name, $args)
    {
        return call_user_func_array([$this->user, $name], $args);
    }

    public function extraGet($name)
    {
        return $this->user->{$name};    
    }
}

当我创建Client 的新实例时,它会自动创建User 实例并设置它。但是,我在 __set 中有测试代码,但它没有运行。

感谢任何帮助。

谢谢

【问题讨论】:

    标签: php


    【解决方案1】:

    The __set magic method is called only when a property is inaccessible from the context in which it is set。因为Client扩展了Part,所以Part的属性都可以在Client中访问,所以不需要魔法方法。

    【讨论】:

    • 谢谢,不过还是有点困惑。我该如何解决这个问题?
    • @cheese5505 这完全取决于您需要做什么。如果您真的需要神奇的工作方法。
    • 我需要能够运行$client-&gt;user 来检索$client-&gt;attributes['user'] 以及$client-&gt;user = etc 来设置$client-&gt;attributes['user']。哪个是我最好的选择?你能不能也给我指出做这件事的方向?谢谢!
    • @cheese5505 老实说,你可能要三思而后行。事实上,我今晚正在处理一些非常相似的代码,这让我的臼齿受伤了......我可能会使用特定于属性的 getter 和 setter,这样很明显你正在通过调用这些方法来做一些特别的事情。否则,这种事情会导致非常微妙的错误。
    猜你喜欢
    • 2011-06-10
    • 1970-01-01
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-02
    • 1970-01-01
    相关资源
    最近更新 更多