代码之家  ›  专栏  ›  技术社区  ›  Michael Ekoka

哪个PHP接口允许使用数组表示法访问对象的属性?

  •  2
  • Michael Ekoka  · 技术社区  · 15 年前

    哪个PHP SPL接口允许对象执行此操作:

    $object->month = 'january';
    echo $object['month']; // january
    
    $record['day'] = 'saturday';
    echo $record->day; // saturday
    

    e、 g.例如在图书馆,如条令(条令记录)

    $object = new ArrayObject();
    $object['a'] = 'test';
    $object['a'] == $object->a; // false
    

    编辑:

    class Arrayable implements ArrayAccess
    {
        protected $container = array();
    
        # implement ArrayAccess methods to allow array notation 
        # $object = new Arrayable();
        # $object['value'] = 'some data';
    
        function offsetExists($offset)
        {
            return isset($this->container[$offset]);
        }
    
        function offsetGet($offset)
        {
            return $this->container[$offset];
        }
    
        function offsetSet($offset, $value)
        {
            $this->container[$offset] = $value;
        }
    
        function offsetUnset($offset)
        {
            unset($this->container[$offset]);
        }
    
        # now, force $object->value to map to $object['value'] 
        # using magic methods
    
        function __set($offset, $value)
        {
            $this->offsetSet($offset, $value);
        }
    
        function __get($offset)
        {
            return $this->offsetGet($offset); 
        }
    }
    
    5 回复  |  直到 15 年前
        1
  •  4
  •   webketje    10 年前

    它是 ArrayAccess

    sourcecode for Doctrine_Record

    abstract class Doctrine_Record 
        extends Doctrine_Record_Abstract 
        implements Countable, IteratorAggregate, Serializable
    

    Doctrine_Record_Abstract

    abstract class Doctrine_Record_Abstract extends Doctrine_Access
    

    最后 Doctrine_Access

    abstract class Doctrine_Access 
        extends Doctrine_Locator_Injectable 
        implements ArrayAccess
    

    来自DocBlock

    为条令子类提供数组访问和属性重载接口


    abstract public boolean offsetExists  ( mixed $offset  );
    abstract public mixed offsetGet ( mixed $offset );
    abstract public void offsetSet ( mixed $offset , mixed $value );
    abstract public void offsetUnset ( mixed $offset );
    

    PHP手册中有一个基本用法示例(链接如上)

        2
  •  3
  •   Gordon Haim Evgi    15 年前

    您在这里使用的是两种不同的东西:

    的ArrayAccess接口 $a[key] http://php.net/manual/en/language.oop5.overloading.php $a->key

    发生的是

    $a[钥匙] $a->offsetGet(key) $a->钥匙 $a->__get(key) $a->__set(key, val) (在类似的情况下) $a->key = val

        3
  •  0
  •   aviv    15 年前

    我认为您可以强制转换对象和数组。。

    $object = (object)array('name'=>'aviv');
    echo $object->name; // prints aviv
    

    反之亦然。。

    $array= (array)$object;
    echo $array['name']; // prints aviv
    
        4
  •  0
  •   Michal Ciechan    15 年前

    您可以实现自己的类 例如

    class PropertyTest {
     $month;
    }
    

    然后在代码使用上

    $object = new PropertyTest;
    $object->month = "January";
    echo $obejct->month;
    
        5
  •  0
  •   Jens A. Koch    8 年前

    <?php
    $object = new ArrayObject([], ArrayObject::ARRAY_AS_PROPS);
    
    $object['a'] = 'test';
    var_dump($object['a'] == $object->a); // expected: bool(true)
    
    $object->month = 'january';
    echo $object['month'];               // expected: january
    
    $object['day'] = 'saturday';
    echo $object->day;                   // expected: saturday
    

    演示 : https://3v4l.org/Nd5NW


    ArrayObject 接受第二个构造函数参数,该参数为

    • ArrayObject::标准属性列表 对象的属性在作为列表(var_dump、foreach等)访问时具有正常的功能。

    • ArrayObject::数组作为道具 条目可以作为属性(读和写)访问。

    : http://php.net/manual/de/class.arrayobject.php