LoginSignup
5
4

More than 5 years have passed since last update.

staticではないメソッドをis_callableに渡した時のバージョンによる違い

Last updated at Posted at 2014-08-01

PHP5.4ではstaticではないメソッドを
is_callable("クラス名::メソッド名")でチェックしてもtrueとなりますが、
PHP5.2ではfalseとなります。

PHP5.4

php > class Foo { function test() { echo 1; } static function static_test() { echo 2; } function is_callable($name) { return is_callable($name);} }
php > $f = new Foo;
php > echo $f->is_callable("test");
php > echo $f->is_callable("Foo::test");
1
php > echo $f->is_callable("Foo::static_test");
1
php > call_user_func_array("Foo::test", array());
1
php > call_user_func_array("Foo::static_test", array());
2

PHP5.2

php > class Foo { function test() { echo 1; } static function static_test() { echo 2; } function is_callable($name) { return is_callable($name);} }
php > $f = new Foo;
php > echo $f->is_callable("test");
php > echo $f->is_callable("Foo::test");
php > echo $f->is_callable("Foo::static_test");
1
php > call_user_func_array("Foo::test", array());
PHP Warning:  call_user_func_array(): First argument is expected to be a valid callback, 'Foo::test' was given in php shell code on line 1

Warning: call_user_func_array(): First argument is expected to be a valid callback, 'Foo::test' was given in php shell code on line 1
php > call_user_func_array("Foo::static_test", array());
2
5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4