Created
November 27, 2012 13:10
-
-
Save hirak/4154160 to your computer and use it in GitHub Desktop.
PHPで固定型のコレクションクラスを作る ref: http://qiita.com/items/bfab969ee5424e4da734
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // intだけに制限する | |
| class IntArray extends ArrayObject | |
| { | |
| function offsetSet($offset, $value) { | |
| if (is_int($value)) { | |
| return parent::offsetSet($offset, $value); | |
| } | |
| throw new InvalidArgumentException; | |
| } | |
| } | |
| // Hogeクラスのインスタンスだけに制限する | |
| class HogeArray extends ArrayObject | |
| { | |
| function offsetSet($offset, $value) { | |
| if ($value instanceof Hoge) { | |
| return parent::offsetSet($offset, $value); | |
| } | |
| throw new InvalidArgumentException; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment