What do you think of my approach to emulating a nested class??
GOAL:
class Foo {
private $bars = [];
protected $secret;
public function __construct() {
$this->secret = true;
}
private class Bar {
public function getSecret() {
return parent::$secret;
}
}
public function newBar() {
$this->bars[] = new Bar;
}
}
IMPLEMENTATION:
class Foo {
private $bars;
protected $secret;
public function __construct() {
$this->secret = true;
}
public function newBar( Bar $bar = null ) {
if( $bar === null ) {
return new Bar( $this );
}
else {
if( $bar->getFoo() === null )
return ($this->bars[] = $bar);
else throw new Exception;
}
}
public function getSecret( Bar $bar ) {
if( in_array( $bar, $this->bars ) )
return $this->secret;
else throw new Exception;
}
}
class Bar {
private $foo;
public function __construct( Foo $foo ) {
$foo->newBar( $this );
$this->foo = $foo;
}
public function getSecret() {
return $this->foo->getSecret( $this );
}
public function getFoo() {
return $this->foo;
}
}