class fixIndex implements ArrayAccess {
private $container = array(); private $default_return = false;
function __construct( &$arr , $def_return = false ) {
$this->container = &$arr;
$this->default_return = $def_return;
}
function __get($key) {
return $this->container[ $key ];
} else {
//echo 'taki index nie istnieje<br>';
return false;
}
}
function __set($key, $val) {
$this->container[ $key ] = $key;
}
function __call($name,$args) {
return false;
}
function offsetExists($key) {
return true;
} else {
return false;
}
}
function offsetGet($key) {
return $this->container[ $key ];
} else {
echo '<br><b>taki index ['.$key.'] nie istnieje</b>'; return $this->default_return;
}
}
function offsetSet($key, $var) {
$this->container[ $key ] = $var;
}
function offsetUnset($key) {
unset( $this->container[ $key ] ); }
}
$oArray['name'] = 'name';
$oArray['test'] = array('cos'=> 'cosx');
$aTestowa = new fixIndex($oArray);
echo "<br> aTestowa[name] = ".$aTestowa['name']; //dziala echo "<br> aTestowa[brak] = ".$aTestowa['brak']; //dziala echo "<br> aTestowa[test][cos] = ".$aTestowa['test']['cos']; //dziala echo "<br> aTestowa[brak][brakujacy] = ".$aTestowa['brak']['brakujacy']; //dziala echo "<br> aTestowa[test][xxxxxx] = ".$aTestowa['test']['xxxxxx']; //NIE dziala
mam taką specyficzną sytuację, że jak indexu nie ma to sobie to loguje (w miejscu tych 'echo').
Działa to tak jak pisałem - dla tablic jednowymiarowych, przynajmniej mi tak z testów wyszło.
Ten ostatni przykład - z indexem 'xxxxxx', jak to zmodyfikować?
Jak zrobiłem coś źle to proszę o pomoc.
to $default_return zignorujcie