Quale interfaccia PHP permette proprietà degli oggetti siano accessibili con la notazione array?
-
22-09-2019 - |
Domanda
Quali un'interfaccia PHP SPL permette agli oggetti di fare questo:
$object->month = 'january';
echo $object['month']; // january
$record['day'] = 'saturday';
echo $record->day; // saturday
es. come ad esempio nelle biblioteche, come Doctrine (Doctrine_Record)
come faccio a implementare questo? Ho provato con ArrayObject, ma non si comportano come ho pensato che.
cioè.
$object = new ArrayObject();
$object['a'] = 'test';
$object['a'] == $object->a; // false
Modifica
Ho provato un'implementazione barebone che ho chiamato 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);
}
}
Soluzione
E ' ArrayAccess
Vedere la codice sorgente di Doctrine_Record
abstract class Doctrine_Record
extends Doctrine_Record_Abstract
implements Countable, IteratorAggregate, Serializable
abstract class Doctrine_Record_Abstract extends Doctrine_Access
abstract class Doctrine_Access
extends Doctrine_Locator_Injectable
implements ArrayAccess
Da DocBlock
Fornisce accesso agli array e l'interfaccia di sovraccarico di proprietà per le sottoclassi Dottrina
Un oggetto che implementa ArrayAccess deve avere questi metodi
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 );
C'è un esempio di utilizzo di base nel manuale di PHP (linkato sopra)
Altri suggerimenti
Si utilizza due cose diverse qui:
L'interfaccia ArrayAccess per $a[key]
e
http://php.net/manual/en/language.oop5.overloading. php per $a->key
Quello che succede è
$a[key]
chiamerà $a->offsetGet(key)
(ereditato da ArrayAccess) e $a->key
chiamerà $a->__get(key)
o $a->__set(key, val)
(in contesti come $a->key = val
).
Penso che si possa lanciare oggetti e gli array ..
$object = (object)array('name'=>'aviv');
echo $object->name; // prints aviv
E viceversa ..
$array= (array)$object;
echo $array['name']; // prints aviv
È possibile implementare la propria classe per esempio.
class PropertyTest {
$month;
}
allora in uso codice
$object = new PropertyTest;
$object->month = "January";
echo $obejct->month;
Sto rispondendo alla domanda utilizzando il codice di esempio con una piccola aggiunta:
<?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
Demo : https://3v4l.org/Nd5NW
ArrayObject
accetta un secondo argomento del costruttore, che è o
-
ArrayObject :: STD_PROP_LIST Proprietà dell'oggetto hanno la loro funzionalità normale quando si accede come lista (var_dump, foreach, ecc.).
-
ArrayObject :: ARRAY_AS_PROPS Le iscrizioni possono essere accessibili come proprietà (leggere e scrivere).
Riferimenti : http://php.net/ manuale / de / class.arrayobject.php