Skip to content

Instantly share code, notes, and snippets.

@mikeschinkel
Created June 22, 2012 21:57
Show Gist options
  • Save mikeschinkel/2975437 to your computer and use it in GitHub Desktop.
Save mikeschinkel/2975437 to your computer and use it in GitHub Desktop.
Proof of concept work for replacing WordPress' $menu variable with a class that implements ArrayAccess and Iterator.
<?php
class Array_Simulator implements ArrayAccess, Iterator {
protected $menu;
public function offsetUnset( $key ) {
if ( $this->offsetExists( $key ) )
unset( $this->menu[$key] );
}
public function offsetSet( $key, $value ) {
if ( is_null( $key ) ) {
$this->menu[] = $value;
} else {
$this->menu[$key] = $value;
}
}
public function offsetGet( $key ) {
if ( ! $this->offsetExists( $key ) )
throw new Exception( sprintf( __( "Offset for ['%s'] doesn't exist on access.", 'sunrise' ), $key ) );
return $this->menu[$key];
}
public function offsetExists( $key ) {
return isset( $this->menu[$key] );
}
public function rewind() {
reset( $this->menu );
}
public function current() {
return current( $this->menu );
}
public function key() {
return key( $this->menu );
}
public function next() {
return next( $this->menu );
}
public function valid() {
return false !== current( $this->menu );
}
}
$menu = new Array_Simulator();
$menu[] = 'Test1';
$menu[] = 'Test2';
$menu[] = 'Test3';
$menu['foo'] = 'Bar';
header( 'Content-type: text/plain' );
print_r( $menu );
unset( $menu['foo'] );
foreach( $menu as $item ) {
echo "\n{$item}";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment