Last active
October 24, 2018 15:13
-
-
Save yoshikyoto/bfa85100bb9dd41359fa6c4a046a31c8 to your computer and use it in GitHub Desktop.
PHPのPropertyとCallable
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 | |
/** | |
* callableなものをプロパティに突っ込んだときの挙動が面白い | |
* PHP >= 7 | |
*/ | |
class FuncClass { | |
private $func; | |
private function func() { | |
echo 'これはメソッド' . PHP_EOL; | |
} | |
public function __construct() { | |
$this->func = function() { | |
echo 'これはプロパティとして定義された関数' . PHP_EOL; | |
}; | |
} | |
public function exec() { | |
echo '$this->func()' . PHP_EOL; | |
$this->func(); // 「これはメソッド」 と出力される | |
echo '($this->func)()' . PHP_EOL; | |
($this->func)(); // 「これはプロパティとして定義された関数」 と出力される | |
} | |
} | |
$i = new FuncClass(); | |
$i->exec(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment