独立してオーバーロードされたプロパティ

__getおよび__setメソッドを使用してプロパティをオーバーロードする標準的なメカニズムは、実際の使用にはあまり便利ではありませんが、それを使用すると、プロパティを操作するための便利なdslを作成できます。 すぐに使用例(以下、 適応型付けパターンが使用されます。事前に慣れておくことをお勧めします):



class Title extends ProtoObject {<br> protected $_text= '' ;<br> function set_text( $val ){<br> return $ this ->aTitleString( $val );<br> }<br> function get_text( $val ){<br> if ( empty( $val ) ) return '[untitled]' ;<br> return $val;<br> }<br><br> function aTitleString( $val ){<br> aString( &$val );<br> if ( strlen( $val ) > 255 ) $val= substr( $val, 0, 252 ) . '...' ;<br> return $val;<br> }<br>}<br><br>$title= new Title;<br>$title->text= 123;<br>var_dump( $title->text ); // string(3) "123" <br>var_dump( $title->text( '' )->text() ); // string(10) "[untitled]" <br>echo $title;<br> // Title Object <br> // ( <br> // [_text:protected] => <br> // )







建築



フィールド名はアンダースコアで始まる必要があり、アクセサーには適切なプレフィックスが必要です。 プロパティ固有のgetterおよび/またはsetterを指定しない場合、共通のget_およびset_が使用されます。 アクセサインターフェイスはシンプルです。渡される値を変換します。 新しい値がセッターに転送され、結果がフィールドに保存されます。 ゲッターでは、保存された値が入力に入り、結果が出力されます。 実際、アクセサーはプレフィルターおよびポストフィルターとして機能します。



プロパティは、フィールドとしてだけでなく、ポリモーフィック関数としてもアクセスできます。 引数を渡さない場合、1つのパラメーターを渡すと、「チェーン」をサポートするセッターのように、ゲッターのように機能します。 さらにパラメーターを渡す場合は、問題を待ちます;-)プロパティの名前と一致しない名前を持つ未知のメソッドへの呼び出しをインターセプトするには、デフォルトで単純に例外をスローする_callメソッドをオーバーロードできます。



Little bun-デフォルトでオブジェクトを文字列に変換すると、print_rでオブジェクトがダンプされます。 一般に、__ toStringの推奨事項は、このメソッドがオブジェクトの内部状態を最も完全に反映する文字列を返すことです。



いくつかのプロパティの例



protected $_count= 0;<br> function set_count( $val ){<br> $ this ->_message= null ;<br> return anUnsignedInt( $val );<br> }



負でない整数値を格納します。 '-2'と2.1の形式の値は2に変換されます。ブール値、または文字を含む文字列を渡すと、例外がスローされます。 anUnsignedInt typcasterを自分で実装することを提案します。



protected $_message;<br> function set_message( $val ){<br> throw new Exception( 'message is autogenerated property' );<br> }<br> function get_message( $val ){<br> if ( empty( $val ) ) $val= $ this ->_message= $ this ->title . ': ' . $ this ->count;<br> return $val;<br> }



怠zyなプロパティ。 他のフィールドの値の関数であるため、値を手動で設定することはできません。 ただし、このプロパティは計算された値をキャッシュするため、依存するフィールドのセッターでキャッシュリセットを指定する必要があります。



protected $_point;<br> function set_point( $val ){<br> return Point::anInstance( $val );<br> }<br> function get_point( $val ){<br> return clone $val;<br> }



インターフェイスを使用してオブジェクトを保存します。 オブジェクトが渡されない場合、Pointは対応するパラメーターでインスタンス化します。 パラメーターが正しくない場合、例外。 プロパティを読み取ると、クローンのみが返され、保存されたオブジェクトはそのままの状態でプライベートになります。 typcasterの実装は宿題のままにします;-)



さて、初心者向け-「型付き変数」クラス:

class Vary extends ProtoObject {<br><br> protected $_type;<br> function set_type( $type ){<br> aString( &$type );<br> if ( !function_exists( $type ) ) throw new Exception( 'unknown type' );<br> if ( $ this ->type ):<br> $ this ->_type= $type;<br> $ this ->val= $ this ->val;<br> endif;<br> return $type;<br> }<br><br> protected $_val;<br> function set_val( $val ){<br> return $val= call_user_func( $ this ->type, $val );<br> }<br><br> function __construct( $type ){<br> $ this ->type= $type;<br> }<br><br> function __toString( ){<br> return aString( $ this ->val );<br> }<br>}<br><br>$count= new Vary( aString );<br>$count->val= '5cm per second' ;<br>echo $count->type; // aString <br>var_dump( $count->val ); // string(14) "5cm per second" <br>$count->type= aSoftNumber;<br>var_dump( $count->val ); // int(5) <br>echo $count; // 5 <br>echo $count->val( '' ); // 0 <br>var_dump( $count );<br> // object(Vary)#1 (2) { <br> // ["_type:protected"]=> <br> // string(11) "aSoftNumber" <br> // ["_val:protected"]=> <br> // int(0) <br> // }







実際にその機会のヒーロー



class ProtoObject {<br><br> static $version= 8;<br> static $description= 'common object extension' ;<br> static $license= 'public domain' ;<br><br> function __toString( ){<br> return print_r( $ this , true );<br>}<br><br> function __set( $name, $value= null ){<br> $ this ->_aPropertyName( &$name );<br> $method= 'set' . $name;<br> if ( !method_exists( $ this , $method ) ) $method= 'set_' ;<br> $value= $ this ->{ $method }( $value );<br> $ this ->{ $name }= $value;<br> return $ this ;<br>}<br> function set_( $val ){<br> return $val;<br>}<br><br> function __get( $name ){<br> $ this ->_aPropertyName( &$name );<br> $method= 'get' . $name;<br> if ( !method_exists( $ this , $method ) ) $method= 'get_' ;<br> $value= $ this ->{ $name };<br> $value= $ this ->{ $method }( $value );<br> return $value;<br>}<br> function get_( $val ){<br> return $val;<br>}<br><br> function __call( $name, $args ){<br> try {<br> $ this ->_aPropertyName( &$name );<br> } catch ( Exception $e ){<br> return $ this ->_call( $name, $args );<br> }<br> switch ( count( $args ) ){<br> case 0: return $ this ->__get( $name );<br> case 1: return $ this ->__set( $name, $args[0] );<br> default : throw new Exception( 'wrong parameters count' );<br> }<br>}<br> function _call( $name, $args ){<br> throw new Exception( 'method not found' );<br>}<br><br> function _aPropertyName( $val ){<br> if ( $val[0] !== '_' ) $val= '_' . $val;<br> if ( !property_exists( $ this , $val ) ) throw new Exception( 'property not found' );<br> return $val;<br>}<br><br>}






All Articles