考え方は、異なるタイプのオブジェクトを含むコレクションは、たとえば、1つの統一されたコレクションタイプではなく、それ自体で区別されるということです。 つまり、タイプUserのオブジェクトのコレクションは、 Bookのオブジェクトのコレクションと同じではありません。 当然、最初の考えは、コレクション( UserCollection 、 BookCollection 、...)に異なるクラスを作成することでした。 ただし、データアプローチでは必要な柔軟性が得られず、すべてに加えて、そのような各クラスの宣言に時間を費やす必要があります。
少し考えてから、コレクションクラスの動的作成を実装しました。 このように見えます:ユーザーはBook型のオブジェクトのコレクションを作成し、目的のBookCollection型が自動的に作成されます(つまり、宣言されます)。
結果として私が得たもの:
-完全なTypeHintingはコレクションタイプを作成しました。
-コレクションの厳密な類型化。
-C#のように配列としてコレクションにアクセスすることが可能です( ArrayAccessインターフェースを実装することにより)
-コレクションの完全な反復(任意のサイクルで使用する機能)。
実装
回収工場
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
/** * * * @author [x26]VOLAND */ abstract class CollectionFactory { /** * . * * @param string $type * @return mixed */ public static function create($type) { $ class = $type . 'Collection' ; self::__create_class($ class ); $obj = new $ class ($type); return $obj; } /** * $class * * @param string $class * @return void */ private static function __create_class($ class ) { if ( ! class_exists($ class )) { eval( 'class ' . $ class . ' extends Collection { }' ); } } } * This source code was highlighted with Source Code Highlighter .
コレクションクラス(動作を説明)
*このソースコードは、 ソースコードハイライターで強調表示されました。
- / **
 - *コレクションクラス
 - *どのコレクションが作成されるかに基づく基本的なユニバーサルタイプ。
 - *
 - * @author [x26] VOLAND
 - * /
 - 抽象 クラス CollectionはIteratorAggregate、ArrayAccess、Countableを実装します{
 - / **
 - *このコレクションに保存されているアイテムのタイプ。
 - * @var string
 - * /
 - private $ __タイプ;
 - / **
 - *オブジェクトストレージ
 - * @var配列
 - * /
 - private $ __ collection = array();
 - // ------------------------------------------------ --------------------
 - / **
 - *デザイナー。
 - *このコレクションに保存される要素のタイプを指定します。
 - *
 - * @param string $ type要素タイプ
 - * @return void
 - * /
 - パブリック 関数 __construct($タイプ){
 - $ this-> __ type = $ type;
 - }
 - // ------------------------------------------------ --------------------
 - / **
 - *オブジェクトのタイプを確認します。
 - * `foreign`タイプのオブジェクトをコレクションに追加しないようにします。
 - *
 - * @param object $ objectチェックするオブジェクト
 - * @return void
 - * @throws例外
 - * /
 - プライベート 関数 __check_type(&$ object ){
 - if (get_class($ object )!= $ this-> __ type){
 - 新しい例外をスロー ( 「オブジェクト型」 .. get_class($ object )
 - 。 ``タイプのオブジェクトのコレクションに追加できません '' 。 $ this-> __タイプ。 '' ' );
 - }
 - }
 - // ------------------------------------------------ --------------------
 - / **
 - *引数に渡されたオブジェクトをコレクションに追加します。
 - *
 - * @param object(s)オブジェクト
 - * @return mixedコレクション
 - * /
 - パブリック 関数 add()
 - {
 - $ args = func_get_args();
 - foreach ($ args as $ object ){
 - $ this-> __ check_type($ object );
 - $ this-> __ collection [] = $ object ;
 - }
 - $ thisを 返します。
 - }
 - // ------------------------------------------------ --------------------
 - / **
 - *引数から渡されたオブジェクトをコレクションから削除します。
 - *
 - * @param object(s)オブジェクト
 - * @return mixedコレクション
 - * /
 - パブリック 関数 remove()
 - {
 - $ args = func_get_args();
 - foreach ($ args as $ object ){
 - unset($ this-> __ collection [array_search($ object 、$ this-> __ collection)]);
 - }
 - $ thisを 返します。
 - }
 - // ------------------------------------------------ --------------------
 - / **
 - *コレクションをクリーンアップします。
 - *
 - * @return mixedコレクション
 - * /
 - パブリック 関数 clear(){
 - $ this-> __ collection = array();
 - $ thisを 返します。
 - }
 - // ------------------------------------------------ --------------------
 - / **
 - *コレクションが空かどうかを調べます。
 - *
 - * @return bool
 - * /
 - パブリック 関数 isEmpty(){
 - 空を返す ($ this-> __コレクション);
 - }
 - // ------------------------------------------------ --------------------
 - / **
 - * IteratorAggregateインターフェースの実装
 - * /
 - / **
 - *イテレータオブジェクトを返します。
 - *
 - * @return CollectionIterator
 - * /
 - パブリック 関数 getIterator(){
 - 新しい CollectionIteratorを返す ($ this-> __コレクション);
 - }
 - // ------------------------------------------------ --------------------
 - / **
 - * ArrayAccessインターフェースの実装。
 - * /
 - / **
 - *コレクションの要素をオフセットに設定します
 - *
 - * @param ineter $ offsetオフセット
 - * @param mixed $ offsetオブジェクト
 - * @return void
 - * /
 - public function offsetSet($ offset、$ object ){
 - $ this-> __ check_type($ object );
 - if ($ offset === NULL){
 - $ offset = max(array_keys($ this-> __ collection))+ 1;
 - }
 - $ this-> __ collection [$ offset] = $ object ;
 - }
 - // ------------------------------------------------ --------------------
 - / **
 - *指定されたキーを持つアイテムが存在するかどうかを調べます。
 - *
 - * @param integer $ offsetキー
 - * @return bool
 - * /
 - パブリック 関数 offsetExists($ offset){
 - return isset($ this-> __ collection [$ offset]);
 - }
 - // ------------------------------------------------ --------------------
 - / **
 - * $ offsetキーによって参照される要素を削除します。
 - *
 - * @param integer $ offsetキー
 - * @return void
 - * /
 - パブリック 関数 offsetUnset($ offset){
 - 設定解除($ this-> __コレクション[$オフセット]);
 - }
 - // ------------------------------------------------ --------------------
 - / **
 - *キーでアイテムを返します。
 - *
 - * @param integer $ offsetキー
 - * @return mixed
 - * /
 - パブリック 関数 offsetGet($ offset){
 - if (isset($ this-> __ collection [$ offset])=== FALSE){
 - NULLを返します。
 - }
 - $ this-> __コレクション[$オフセット]を返します。
 - }
 - // ------------------------------------------------ --------------------
 - / **
 - * Countableインターフェースの実装
 - * /
 - / **
 - *コレクション内の要素の数を返します。
 - *
 - * @return integer
 - * /
 - パブリック 関数カウント(){
 - return sizeof ($ this- > __コレクション);
 - }
 - }
 
使用例
*このソースコードは、 ソースコードハイライターで強調表示されました。
- <?php
 - クラス BookStore {
 - 関数 addBooks(BookCollection $ books){
 - //実装
 - }
 - 関数 addMagazines(MagazineCollection $ magazines){
 - //実装
 - }
 - 関数 addGoods(コレクション$グッズ){
 - //コレクションのタイプが重要でない場合、
 - //コレクションのベースタイプを指定できます
 - }
 - }
 - クラス Book {
 - var $ id;
 - function Book($ id){
 - $ this-> id = $ id;
 - }
 - }
 - クラス Magazine {
 - var $ id;
 - function Magazine($ id){
 - $ this-> id = $ id;
 - }
 - }
 - //コレクションを作成します
 - $ books = CollectionFactory :: create( 'Book' );
 - echo get_class($本); // BookCollection
 - //オブジェクトをコレクションに追加します。
 - $ books-> add( new Book(1)、 new Book(2));
 - $ books-> add( new Book(3))-> add( new Book(2));
 - $本[] = 新しい本(5);
 - エコー数($本); // 5
 - ...
 - foreach ($本を $本として ){
 - echo $ book-> id;
 - } // 12345
 - ...
 - $ books-> add( new Magazine(1)); //エラー(間違ったタイプ)
 - ...
 - $ magazines = CollectionFactory :: create( 'Magazine' );
 - $ magazines-> add( new Magazine(1));
 - ...
 - $ bookStore = new BookStore();
 - $ bookStore-> addBooks($ books); //大丈夫
 - $ bookStore-> addBooks($雑誌); //エラー(間違ったタイプ)
 - $ bookStore-> addMagazines($雑誌); //大丈夫
 - $ bookStore-> addGoods($ books); //大丈夫
 - $ bookStore-> addGoods($雑誌); //大丈夫
 - ?>
 
ソースをダウンロード