Skip to content

Instantly share code, notes, and snippets.

View hirak's full-sized avatar

Hiraku NAKANO hirak

View GitHub Profile
@hirak
hirak / file0.php
Created March 7, 2013 16:27
PHPで列挙型(enum)を作る ref: http://qiita.com/items/71e385b56dcaa37629fe
<?php
abstract class Enum
{
private $scalar;
function __construct($value)
{
$ref = new ReflectionObject($this);
$consts = $ref->getConstants();
if (! in_array($value, $consts, true)) {
@hirak
hirak / file0.php
Created December 10, 2012 14:44
staticメソッドは$this->でも呼べる ref: http://qiita.com/items/0e2364e362143f348d53
class Hoge {
static function foo() {
echo 'Hoge::foo';
}
}
$hoge = new Hoge;
$hoge->foo(); //一切エラーは発生しない
@hirak
hirak / file0.js
Created November 27, 2012 15:25
ブロックコメントの工夫 ref: http://qiita.com/items/de8c44520d5aa9268ac7
/*
hogehoge();
fugafuga();
//*/
@hirak
hirak / file0.php
Created November 27, 2012 13:10
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;
}
@hirak
hirak / file0.txt
Created November 10, 2012 06:01
prototypeを上書きする弊害 ref: http://qiita.com/items/2d95fc57a76f3ca29eb6
function Klass(){}
Klass.prototype.foo = 'foo';
var o = new Klass;
console.log(o.constructor.name); //Klass
@hirak
hirak / file0.php
Created October 31, 2012 22:34
new演算子が作用できるもの ref: http://qiita.com/items/8ebed8e3b0b4be776572
<?php
class A {
}
$classname = 'A';
$a = new A; //Aオブジェクト1
$b = new $classname; //Aオブジェクト2
$c = new $a; //Aオブジェクト3
function Class(c) {
if (c == null) {
c = function(){};
}
c.extend = _extends;
c.mixin = _mixin;
c.statics = _static;
return c;
}
function _extends(uber) {
@hirak
hirak / file0.txt
Created May 4, 2012 05:22
globalオブジェクトを取得する ref: http://qiita.com/items/d249a2f2f13532748324
var global = this;
@hirak
hirak / file0.txt
Created May 3, 2012 13:09
名前空間用のオブジェクトを作る関数 ref: http://qiita.com/items/ae2639e0b91af0186b08
function namespace(str) {
var names=str.split(".")
, i, l, cur = Function("return this")()
;
for (i=0,l=names.length; i<l; i++) {
cur[names[i]] = cur[names[i]] || {};
cur = cur[names[i]];
}
return cur;
}
var a = "global";
with ({a:"local"}) {
console.log(a); //"local"
with ({a:"local2"}) { /* 入れ子にできる */
console.log(a); //"local2"
}
console.log(a); //"local"
}
console.log(a); //"global"