Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What is going on with this article?

More than 3 years have passed since last update.

@kure

__invoke()の小さなサンプル

コード

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
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
0
Help us understand the problem. What is going on with this article?