0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

__invoke()の小さなサンプル

Posted at

コード

test.php
<?php

class Test{
        public function show($param){
                echo "test: ".$param.PHP_EOL;
        }
}

$test = new Test();
array_map(array($test, "show"), array(1,2,3));


class Test2{
        public function show($param){
                echo "test: ".$param.PHP_EOL;
        }

        public function show2($param){
                        echo "test2: ".$param.PHP_EOL;
        }

        //ここで->show2を指定したらそっちが呼ばれる
        public function __invoke($param){
                return $this->show($param);
        }
}

$test2 = new Test2();
array_map($test2, array(4,5,6));

実行

test: 1
test: 2
test: 3
test: 4
test: 5
test: 6
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?