Created
December 20, 2011 00:55
-
-
Save k-holy/1499683 to your computer and use it in GitHub Desktop.
callableタイプヒントの追加と配列から文字列への暗黙の変換時の警告 (PHP5.4 Advent Calendar 2011 Day 21)
This file contains 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 | |
set_error_handler(function($errno, $errstr, $errfile, $errline){ | |
$titles = array ( | |
E_ERROR => 'Fatal error', | |
E_WARNING => 'Warning', | |
E_NOTICE => 'Notice', | |
E_STRICT => 'Strict standards', | |
E_RECOVERABLE_ERROR => 'Catchable fatal error', | |
E_DEPRECATED => 'Depricated', | |
); | |
echo sprintf('<p>%s[%d]: %s</p>', | |
(isset($titles[$errno])) ? $titles[$errno] : 'Unknown error', | |
$errno, htmlspecialchars($errstr, ENT_QUOTES, 'UTF-8')); | |
return true; | |
}); |
This file contains 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 | |
$array = array(); | |
echo $array; | |
// Notice[8]: Array to string conversion | |
// Array | |
'text' . $array; | |
// Notice[8]: Array to string conversion | |
var_dump((string)$array); | |
// Notice[8]: Array to string conversion | |
// string(5) "Array" | |
var_dump(strlen(sprintf('%s', $array))); | |
// Notice[8]: Array to string conversion | |
// int(5) | |
$array = new \ArrayObject(); | |
var_dump((string)$array); | |
// Catchable fatal error[4096]: Object of class ArrayObject could not be converted to string | |
// string(0) "" |
This file contains 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 | |
namespace Acme; | |
class U { | |
public static function exec($var, callable $callback) { | |
return $callback($var); | |
} | |
public static function foo($var) { | |
return sprintf('%s! foo!', $var); | |
} | |
public function bar($var) { | |
return sprintf('%s! bar!', $var); | |
} | |
public function __invoke($var) { | |
return sprintf('%s! invoke!', $var); | |
} | |
} | |
$text = '12345'; | |
// 関数名の文字列 | |
echo U::exec($text, 'number_format'); | |
// 12,345 | |
// (PHP5.3の場合) Catchable fatal error[4096]: Argument 2 passed to Acme\U::exec() must be an instance of Acme\callable, string given, called in *** on line ** and defined | |
// スタティックメソッドを指定したcallback配列 | |
echo U::exec($text, array(__NAMESPACE__ . '\U', 'foo')); | |
// 12345! foo! | |
// インスタンスメソッドを指定したcallback配列 | |
echo U::exec($text, array(new U(), 'bar')); | |
// 12345! bar! | |
// 無名関数(Closure) | |
echo U::exec($text, function($var) { | |
return sprintf('%s! closure!', $var); | |
}); | |
// 12345! closure! | |
// __invoke()実装クラスのインスタンス | |
echo U::exec($text, new U()); | |
// 12345! invoke! | |
// callableではない(存在しないクラスのスタティックメソッドを指定したcallback配列) | |
echo U::exec($text, array('U', 'foo')); | |
// E_RECOVERABLE_ERROR が発生 | |
// Catchable fatal error[4096]: Argument 2 passed to Acme\U::exec() must be callable, array given, called in *** on line *** and defined | |
// Fatal error: Class 'U' not found in... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment