PHP:我可以在方法重载(__get)中获取数组功能吗?

发布于 2024-12-08 02:23:26 字数 248 浏览 0 评论 0原文

我想要实现的是,当我调用 $obj->CAT[15]; 时 $obj 会检查属性 CAT 是否存在,如果不存在,则动态获取值,

public function __get($var){
if($var == "CAT") return $this->cats->get_cat($cat_id);
}

所以我的问题是...如何从我的示例中获取数组的值 15?将其传递给我的 get_cat 方法?

What I'd like ti achieve is that when I call $obj->CAT[15];
the $obj would check if the property CAT exists if not, get the value on the fly

public function __get($var){
if($var == "CAT") return $this->cats->get_cat($cat_id);
}

so my question is... how to get array's value 15 from my example? to pass it to my get_cat method?

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

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

发布评论

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

评论(3

萌面超妹 2024-12-15 02:23:26

__get 返回 ArrayAccess它将委托给其 offsetGet 方法中的 get_cat

像这样的事情:

class CachedCategories implements ArrayAccess
{
  private $memcachedClient;

  public function __construct($memcachedClient)
  {
    $this->memcachedClient = $memcachedClient;
  }

  // Called when using `$cats[18] = "foo"`
  public function offsetSet($key, $value)
  {
    $this->memcachedClient->set($key, $value);
  }

  // Called when using `$cat = $cats[18]`
  public function offsetGet($key)
  {
    return $this->memcachedClient->get($key);
  }

  // Called when using `isset($cats[18])`
  public function offsetExists($key)
  {
    return $this->memcachedClient->get($key) !== false;
  }

  // Called when using `unset($cats)`
  public function offsetUnset($key)
  {
    $this->memcachedClient->delete($key);
  }
}

$cats = new CachedCategories($someMemcachedClient);
$cats[18];

Have __get return an instance of ArrayAccess which will delegate to get_cat in its offsetGet method.

Something like this:

class CachedCategories implements ArrayAccess
{
  private $memcachedClient;

  public function __construct($memcachedClient)
  {
    $this->memcachedClient = $memcachedClient;
  }

  // Called when using `$cats[18] = "foo"`
  public function offsetSet($key, $value)
  {
    $this->memcachedClient->set($key, $value);
  }

  // Called when using `$cat = $cats[18]`
  public function offsetGet($key)
  {
    return $this->memcachedClient->get($key);
  }

  // Called when using `isset($cats[18])`
  public function offsetExists($key)
  {
    return $this->memcachedClient->get($key) !== false;
  }

  // Called when using `unset($cats)`
  public function offsetUnset($key)
  {
    $this->memcachedClient->delete($key);
  }
}

$cats = new CachedCategories($someMemcachedClient);
$cats[18];
<逆流佳人身旁 2024-12-15 02:23:26
class Test
{
    protected $cats;
    function __construct()
    {
        $this->cats = new ArrayObject(); // or your own implementation
        $this->cats[33] = "hey";
    }
    public function __get($name)
    {
        if($name == "CAT") return $this->cats;
    }
}

$a = new Test();
echo $a->CAT[33];

请参阅 http://www.php.net/ArrayAccess 来实现您自己的列表/地图

希望这会有所帮助!

class Test
{
    protected $cats;
    function __construct()
    {
        $this->cats = new ArrayObject(); // or your own implementation
        $this->cats[33] = "hey";
    }
    public function __get($name)
    {
        if($name == "CAT") return $this->cats;
    }
}

$a = new Test();
echo $a->CAT[33];

see http://www.php.net/ArrayAccess to implement your own list/map

hope this helps!

一梦浮鱼 2024-12-15 02:23:26

如果我正确理解你想要实现的目标,你可以使用闭包:

class kittengarten
{
   var $cats;
   function __construct()
   {
      $this->cats[0]='Jerry';
      $this->cats[1]='John';
      $this->cats[2]='Barack';
   }
   public function __get($var)
   {
      if($var == "CAT")
      {
         $array_of_cats=$this->cats;
         return function($num) use ($array_of_cats)
         {
            return $array_of_cats[$num];
         };
      }
   }

}
$kittengarten=new kittengarten();
echo 'The third directly accessed cat is '.$kittengarten->cats[2];
$cat=$kittengarten->CAT;
if (is_string($cat)) echo $kittengarten->CAT; 
else echo 'The third cat accessed using closure is '.$cat(2);

输出:

第三个直接访问的猫是巴拉克

使用闭包访问的第三只猫是巴拉克

If I understood correctly what you want to achieve, you could use a closure:

class kittengarten
{
   var $cats;
   function __construct()
   {
      $this->cats[0]='Jerry';
      $this->cats[1]='John';
      $this->cats[2]='Barack';
   }
   public function __get($var)
   {
      if($var == "CAT")
      {
         $array_of_cats=$this->cats;
         return function($num) use ($array_of_cats)
         {
            return $array_of_cats[$num];
         };
      }
   }

}
$kittengarten=new kittengarten();
echo 'The third directly accessed cat is '.$kittengarten->cats[2];
$cat=$kittengarten->CAT;
if (is_string($cat)) echo $kittengarten->CAT; 
else echo 'The third cat accessed using closure is '.$cat(2);

Output:

The third directly accessed cat is Barack

The third cat accessed using closure is Barack

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文