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 February 20, 2014 15:11
SplFixedArrayで"純粋な配列"を作る ref: http://qiita.com/Hiraku/items/471747cacda58070f3a6
<?php
$a1 = [10,20,30]; //配列っぽい配列
$a2 = [2=>10, 0=>20, 1=>30]; //添え字は整数だが順番が変な配列
$a3 = ['a'=>10, 20, 30]; //添え字に文字列が混じってる
@hirak
hirak / typeOfの戻り値
Last active August 29, 2015 13:55
typeofを改善したtypeOf()関数 ref: http://qiita.com/Hiraku/items/87e5d1cdaaa475c80cc2
console.log(typeOf(undefined)); //'undefined'
console.log(typeOf(null)); //'null'
console.log(typeOf("str")); //'string'
console.log(typeOf(new String("str"))); //'String'
console.log(typeOf(true)); //'boolean'
console.log(typeOf(new Boolean(true))); //'Boolean'
console.log(typeOf(1)); //'number'
console.log(typeOf(NaN)); //'NaN'
console.log(typeOf(Infinity)); //'Infinity'
console.log(typeOf(-Infinity)); //'-Infinity'
@hirak
hirak / stream.js
Created February 2, 2014 13:47
CURLOPT_FILEでHTTPレスポンスをファイルに直接書き込んでいくサンプル ref: http://qiita.com/Hiraku/items/9fb7a0df060e0424921d
var http = require('http');
http.createServer(function(req, res){
var i = 0;
var id = 100 * Math.random() | 0;
res.writeHead(200, {'Content-Type': 'text/plain'});
setInterval(function(){
var result = id + ':' + ++i;
console.log(result);
@hirak
hirak / file0.php
Created September 28, 2013 13:56
コンストラクタを簡単に書く ref: http://qiita.com/Hiraku/items/84156e0ed0dc0937e44c
<?php
//こんな風に書けるとドリーム感いっぱい!?
class Point
{
private $x, $y;
public function __construct($this->x, $this->y);
}
@hirak
hirak / config.php
Created August 19, 2013 21:52
グローバル変数を作らずにファイル間で値をやり取りする ref: http://qiita.com/Hiraku/items/551ba8bf15b6b23a12a6
<?php
namespace My\Temp;
if (!function_exists('My\Temp\getConfig')) {
function getConfig() {
static $config;
if (!$config) {
$config = ['hoge' => 'fuga'];
}
@hirak
hirak / file0.php
Created July 25, 2013 22:59
Twitter風の「約○○分前」を計算する ref: http://qiita.com/Hiraku/items/7264ab40bfad3b50819a
<?php
/**
* Twitter風の「約○○分前」を計算する関数
* @param DateTime $target
* @param DateTime $now 基準時間をずらしたい場合に指定。
* @return string
*/
function toLastUpdateString(DateTime $target, DateTime $now=null)
{
if (!$now) $now = new DateTime('@' . $_SERVER['REQUEST_TIME']);
<?php
class A {
private function echoClass() {
echo 'A', PHP_EOL;
}
public function p() {
$this->echoClass();
}
}
@hirak
hirak / file0.php
Created May 15, 2013 14:47
__toString()でExceptionが発生すると死ぬ ref: http://qiita.com/items/195222c4f4f223e435fa
<?php
class Hoge {
function __toString() {
throw new Exception;
}
}
try {
$hoge = new Hoge;
echo $hoge;
@hirak
hirak / file0.php
Created April 10, 2013 16:30
switch文の使いどころの悩み ref: http://qiita.com/items/af2b304650272410ec29
<?php
switch (true) {
case 0:
echo '数字の0';
break;
case '0':
echo '文字列の0';
break;
case '0.0':
echo '文字列の0.0';
@hirak
hirak / file0.php
Last active December 14, 2015 21:39
PHP5.2で遅延静的束縛 ref: http://qiita.com/Hiraku/items/2f4e3c146857010b4ff6
<?php
function doSomething($hoge=__LINE__) {
var_dump($hoge);
}