获得“对过载属性的间接修改没有效果".注意 [英] Getting "Indirect modification of overloaded property has no effect" notice

查看:65
本文介绍了获得“对过载属性的间接修改没有效果".注意的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用注册表来存储一些对象.这是一个简单的Registry类实现.

I want to use a Registry to store some objects. Here is a simple Registry class implementation.

<?php
  final class Registry
  {
    private $_registry;
    private static $_instance;

    private function __construct()
    {
      $this->_registry = array();
    }

    public function __get($key)
    {
      return
        (isset($this->_registry[$key]) == true) ?
        $this->_registry[$key] :
        null;
    }

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

    public function __isset($key)
    {
      return isset($this->_registry[$key]);
    }

    public static function getInstance()
    {
      if (self::$_instance == null) self::$_instance = new self();
      return self::$_instance;
    }
}

?>

当我尝试访问此类时,收到间接修改重载属性无效"的通知.

When I try to access this class, I get "Indirect modification of overloaded property has no effect" notification.

Registry::getInstance()->foo   = array(1, 2, 3);   // Works
Registry::getInstance()->foo[] = 4;                // Does not work

我该怎么办?

推荐答案

已多次将此行为报告为错误:

This behavior has been reported as a bug a couple times:

  • https://bugs.php.net/bug.php?id=42030
  • https://bugs.php.net/bug.php?id=41641

我不清楚讨论的结果是什么,尽管这似乎与按值"和按引用"传递值有关.我在一些类似的代码中找到的解决方案做了一些事情像这样:

It is unclear to me what the result of the discussions was although it appears to have something to do with values being passed "by value" and "by reference". A solution that I found in some similar code did something like this:

function &__get( $index )
{
   if( array_key_exists( $index, self::$_array ) )
   {
      return self::$_array[ $index ];
   }
   return;
}

function &__set( $index, $value )
{
   if( !empty($index) )
   {
      if( is_object( $value ) || is_array( $value) )
      {
         self::$_array[ $index ] =& $value;
      }
      else
      {
         self::$_array[ $index ] =& $value;
      }
   }
}

请注意它们如何使用&__get&__set,以及在分配值时使用& $value.我认为这是使这项工作有效的方法.

Notice how they use &__get and &__set and also when assigning the value use & $value. I think that is the way to make this work.

这篇关于获得“对过载属性的间接修改没有效果".注意的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆