メモっておけばいつか使うかもしれない(使うとは言っていない)
class Person
{
private $secret;
public function __construct($secret)
{
$this->secret = $secret;
}
public function look(Person $person)
{
return $person->secret;
}
public static function lookStatic(Person $person)
{
return $person->secret;
}
}
class Other
{
public static function lookStatic(Person $person)
{
return $person->secret;
}
}
$me = new Person('ブログ');
$brother = new Person('日記');
echo $me->look($brother);
echo Person::lookStatic($me);
echo Other::lookStatic($brother);
結果は上から順に
日記
ブログ
Fatal error: Cannot access private property Person::$secret
staticメソッドのほうはともかく、同じクラスのインスタンス同士でも
privateフィールドにアクセスできるのは知りませんでした。