Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
哪个PHP接口允许对象';要使用数组表示法访问的属性?_Php_Arrays_Spl_Arrayobject - Fatal编程技术网

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

哪个PHP接口允许对象';要使用数组表示法访问的属性?,php,arrays,spl,arrayobject,Php,Arrays,Spl,Arrayobject,哪个PHP SPL接口允许对象执行此操作: $object->month = 'january'; echo $object['month']; // january $record['day'] = 'saturday'; echo $record->day; // saturday e、 g.例如在图书馆,如条令(条令记录) 我如何实现这一点?我尝试过使用ArrayObject,但它们的行为不像我想象的那样 i、 e 编辑: 我尝试了一个我称之为Arrayable的裸骨实现

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

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

$record['day'] = 'saturday';
echo $record->day; // saturday
e、 g.例如在图书馆,如条令(条令记录)

我如何实现这一点?我尝试过使用ArrayObject,但它们的行为不像我想象的那样

i、 e

编辑:

我尝试了一个我称之为Arrayable的裸骨实现

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); 
    }
}
它是

最后

来自DocBlock

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


实现ArrayAccess的对象必须具有以下方法

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手册中有一个基本用法示例(链接在上面)

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

$object = (object)array('name'=>'aviv');
echo $object->name; // prints aviv
反之亦然

$array= (array)$object;
echo $array['name']; // prints aviv

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

$a[key]
的ArrayAccess接口和 对于
$a->key

发生的是


$a[key]
将调用
$a->offsetGet(key)
(继承自ArrayAccess)和
$a->key
将调用
$a->\u get(key)
$a->\u set(key,val)
(在类似
$a->key=val
的上下文中)。

您可以实现自己的类 e、 g

然后在代码使用上

$object = new PropertyTest;
$object->month = "January";
echo $obejct->month;

我使用您的示例代码回答这个问题,并添加了一个小的补充:

<?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

OP想知道如何使用数组符号访问对象,例如除了
$object->month
之外,还有
$object->month
$object = (object)array('name'=>'aviv');
echo $object->name; // prints aviv
$array= (array)$object;
echo $array['name']; // prints aviv
class PropertyTest {
 $month;
}
$object = new PropertyTest;
$object->month = "January";
echo $obejct->month;
<?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