PHP SPL口允许的对象,要做到这一点:

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

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

例如例如在库等原则(Doctrine_Record)

我如何实施这个吗?我已经试过使用ArrayObject,但他们不表现得因为我以为他们会的。

$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); 
    }
}
有帮助吗?

解决方案

这是了ArrayAccess

请参阅源码为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

从文档块

  

提供用于学说子类

数组访问和属性过载接口

一个目的实施了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手册(上面链接)碱性使用例

其他提示

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

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

,会发生什么是

$a[key]将调用$a->offsetGet(key)(来自了ArrayAccess继承)和$a->key将调用$a->__get(key)$a->__set(key, val)(在像$a->key = val上下文)。

我想你可以投对象和数组..

$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

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


ArrayObject 接受第2构造参数,这是不是

  • ArrayObject::STD_PROP_LIST 特性的物体有自己的正常功能进行访问时,作为清单(var_dump、foreach,等等)。

  • ArrayObject就完成这个工作 项可访问性质(阅读和写)。

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top