Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@yoshikyoto
Last active October 24, 2018 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoshikyoto/bfa85100bb9dd41359fa6c4a046a31c8 to your computer and use it in GitHub Desktop.
Save yoshikyoto/bfa85100bb9dd41359fa6c4a046a31c8 to your computer and use it in GitHub Desktop.
PHPのPropertyとCallable
<?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