PHP

call_user_funcの小さなサンプル

More than 1 year has passed since last update.

コード

<?php

function test(){
        echo "func test".PHP_EOL;
}

class TestClass{
        public function __construct(){
                echo "TEST".PHP_EOL;
        }
        public function test(){
                echo "test".PHP_EOL;
        }
}

call_user_func("test");

$testClass = new TestClass();
call_user_func(array($testClass, "test"));

call_user_func(function(){ echo "no name".PHP_EOL; });

実行

func test
TEST
test
no name