クラスメンバーを宣言するのが最善の策です。それが機能しない場合は$this->isInConstructor
、エラーをスローするかどうかを決定するスイッチ ( ?) を使用できます。
一方、メソッドと__get
メソッドを使用し__set
て、両方をラップされたライブラリにマップすることもできます。
class Foo
{
private $library;
private $trustedValues;
public function __construct( array $values )
{
$this->trustedValues = array( 'foo', 'bar', 'baz' );
$this->library = new stdClass();
foreach( $values as $key=>$value )
{
$this->library->$key = $value;
}
}
public function __get( $key )
{
return $this->library->$key;
}
public function __set( $key, $value )
{
if( in_array( $key, $this->trustedValues ) )
{
$this->library->$key = $value;
}
else
{
throw new Exception( "I don't understand $key => $value." );
}
}
}