Im un peu de mal mise en oeuvre du PHP
ArrayAccess interface lorsque les tableaux multi-dimensionnels sont impliqués.
Je peux obtenir ajoute à travailler
$myobject[] = 'new value';
Je peux obtenir des assignations de haut niveau pour travailler
$myobject['index'] = 'New Value';
Je peux accéder aux valeurs imbriquées si elle est définie comme tant theyre
$myobject['index'] = array('zero');
echo $myobject['index'][0];
- $myobject['index'] = array('zero');
- echo $myobject['index'][0];
Cependant, si j'essaie d'affectation à un indice de imbriqués, il semble avoir complètement ignoré si l'indice de haut niveau existe ou non.
$myobject['index']['one'] = 1;
Im commence à me demander si l'interface ArrayAccess est censé être exécuté directement depuis le tout en regardant Ive Ive a découvert des classes comme ArrayObject implémenter l'interface ArrayAccess.
Heres la classe avec mon débogage gauche, peut-on voir quelque chose cloche dans la façon Ive a tenté d'appliquer ce que c'est?
function echo_r($data)
{
echo sprintf('<pre>%s</pre>', print_r($data, true));
}
abstract class identifiable
{
public $id, $name;
}
final class language extends identifiable implements ArrayAccess
{
public $texts = array();
public function load($ini_file)
{
//$this->texts = array_merge_recursive($this->texts, parse_ini_file($ini_file));
return $this;
}
public function __construct($id, $name = 'New Language', $ini_file = null)
{
return $ini_file ? $this->load($ini_file) : $this;
}
public function offsetExists($offset)
{echo "[e:$offset]";
return isset($this->texts[$offset]);
}
public function offsetGet($offset)
{echo "[g:$offset]";
return $this->texts[$offset];
}
public function offsetSet($offset, $value)
{echo "[s:$offset--$value]";
if(empty($offset))
{
$this->texts[] = $value;
return;
}
$this->texts[$offset] = $value;
}
public function offsetUnset($offset)
{echo "[u:$offset]";
unset($this->texts[$offset]);
}
}
$lang = new language('test', 'Test Language', 'languages/test.ini');
$lang[] = 'appended';
//$lang['stuff'] = array();
$lang['stuff'][] = 'multi-stuff';
echo_r($lang);
///////////////////////////////////////////
[s:--appended][g:stuff]
language Object
(
[texts] => Array
(
[0] => appended
)
[id] =>
[name] =>
)
- function echo_r($data)
- {
- echo sprintf('<pre>%s</pre>', print_r($data, true));
- }
-
- abstract class identifiable
- {
- public $id, $name;
- }
-
- final class language extends identifiable implements ArrayAccess
- {
- public $texts = array();
-
- public function load($ini_file)
- {
- //$this->texts = array_merge_recursive($this->texts, parse_ini_file($ini_file));
- return $this;
- }
-
- public function __construct($id, $name = 'New Language', $ini_file = null)
- {
- return $ini_file ? $this->load($ini_file) : $this;
- }
-
- public function offsetExists($offset)
- {echo "[e:$offset]";
- return isset($this->texts[$offset]);
- }
-
- public function offsetGet($offset)
- {echo "[g:$offset]";
- return $this->texts[$offset];
- }
-
- public function offsetSet($offset, $value)
- {echo "[s:$offset--$value]";
- if(empty($offset))
- {
- $this->texts[] = $value;
- return;
- }
- $this->texts[$offset] = $value;
- }
-
- public function offsetUnset($offset)
- {echo "[u:$offset]";
- unset($this->texts[$offset]);
- }
- }
-
-
- $lang = new language('test', 'Test Language', 'languages/test.ini');
- $lang[] = 'appended';
- //$lang['stuff'] = array();
- $lang['stuff'][] = 'multi-stuff';
- echo_r($lang);
-
- ///////////////////////////////////////////
- [s:--appended][g:stuff]
- language Object
- (
- [texts] => Array
- (
- [0] => appended
- )
-
- [id] =>
- [name] =>
- )
Strong with this one, the sudo is.