Skip to content

Instantly share code, notes, and snippets.

@hirak
Created November 27, 2012 13:10
Show Gist options
  • Select an option

  • Save hirak/4154160 to your computer and use it in GitHub Desktop.

Select an option

Save hirak/4154160 to your computer and use it in GitHub Desktop.
PHPで固定型のコレクションクラスを作る ref: http://qiita.com/items/bfab969ee5424e4da734
<?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