【譲渡】PHP5.3パーソナルテストエッセンス機能まとめ
1. スクリプトのディレクトリを見つけるための __DIR__ マジック定数を導入します
echo __DIR__."|".dirname(__FILE__);
echo (1 ?: 0) ."|". ("" ?: 0)."|". var_export(array() ?: array(1), true);
class xxx{ function __call($func, $args){ echo "function name:".$func; echo "</br />"; echo "args:".var_export($args, true); } static function __callStatic($func, $args){ echo "static function name:".$func; echo "</br />"; echo "static args:".var_export($args, true); } } $x = new xxx(); $x->testfunc("oo"); echo "</br />"; xxx::teststatic("xx");
class xx{ static function test(){ echo __METHOD__; } } $c = "xx"; $m = "test"; $c::$m();
class A{ static function whoami(){ echo __CLASS__; } static function say1(){ self::whoami(); } static function say2(){ static::whoami(); } } class B extends A{ static function whoami(){ echo __CLASS__; } } class C extends A{ static function whoami(){ echo __CLASS__; } } B::say1(); C::say2();
$date = strtotime("11-05-17 00:00:00"); echo date("Y-m-d", $date)."|"; $date = date_create_from_format("y-m-d", "11-05-17"); echo $date->format("Y-m-d");
gc_enable(); // 允许垃圾回收 var_dump(gc_enabled()); // true var_dump(gc_collect_cycles()); // 某个元素的清理 gc_disable(); // 禁止垃圾回收
$fubar = "xxx"; $xbar = <<<ONE this is HEREDOC $fubar ONE; $abar = <<<'TWO' this is NOWDOC $fubar TWO; echo $xbar."|".$abar;
$ofun = create_function('$a, $b', 'return $a*$b;'); //不要忘了分号,传统方法 echo $ofun(2, 5); $nfun = function ($a, $b){ return $a*$b; };//不要忘了分号,现代方法 echo $nfun(3, 5);
$arr = array(1, 2, 3, 5, 6, 7); $res = array_filter($arr, function($v) { return $v >4; }); var_dump($res); function countnum(){ $num = 0; $testt = function() use(&$num){ ++$num; echo $num; }; return $testt; } $t = countnum(); $t(); $t();