下記のコードだとEntryのdoit()がエントリーポイントで、ModelのdoSomething()が呼ばれ、パッと見Modelのsave()が呼ばれるように見えるが……
<?php
namespace Sample;
class Entry {
function doit() {
Model::doSomething();
}
public function save() {
echo __METHOD__;
}
}
class Model {
public function doSomething() {
$this->save();
}
public function save() {
echo __METHOD__;
}
}
$entry = new Entry();
$entry->doit();
~ $ php -v
PHP 5.6.29 (cli) (built: Dec 9 2016 07:03:56)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
~ $ php -d error_reporting=0 Entry.php
Sample\Entry::save
~ $ php -d error_reporting=0 Entry.php | cowsay
____________________
< Sample\Entry::save >
--------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
~ $ ./echo-sd --stress \ \ Entry「もしかして……」 Model「私たち………」 「「入れ替わってるーーー!!!」」 | tail -n 7
Entry「もしかして……」
↘
Model「私たち………」
↙
_人人人人人人人人人人人人人人人人人人人_
> 「「入れ替わってるーーー!!!」」 <
 ̄Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^ ̄
これが言いたいだけだった。(ネタ元)
安心してください。staticキーワードがないメソッドの静的呼び出しで警告は出ます。
~ $ php Entry.php
PHP Deprecated: Non-static method Sample\Model::doSomething() should not be called statically, assuming $this from incompatible context in /Users/user1/Entry.php on line 7
Deprecated: Non-static method Sample\Model::doSomething() should not be called statically, assuming $this from incompatible context in /Users/user1/Entry.php on line 7
Sample\Entry::save%
PHP7ならそもそも実行できない。
~ $ brew unlink php56
Unlinking /usr/local/Cellar/php56/5.6.29_5... 17 symlinks removed
~ $ brew link php71
Linking /usr/local/Cellar/php71/7.1.0_11... 39 symlinks created
~ $ php Entry.php
PHP Deprecated: Non-static method Sample\Model::doSomething() should not be called statically in /Users/user1/Entry.php on line 7
Deprecated: Non-static method Sample\Model::doSomething() should not be called statically in /Users/user1/Entry.php on line 7
PHP Fatal error: Uncaught Error: Using $this when not in object context in /Users/user1/Entry.php:17
Stack trace:
#0 /Users/user1/Entry.php(7): Sample\Model::doSomething()
#1 /Users/user1/Entry.php(26): Sample\Entry->doit()
#2 {main}
thrown in /Users/user1/Entry.php on line 17
Fatal error: Uncaught Error: Using $this when not in object context in /Users/user1/Entry.php:17
Stack trace:
#0 /Users/user1/Entry.php(7): Sample\Model::doSomething()
#1 /Users/user1/Entry.php(26): Sample\Entry->doit()
#2 {main}
thrown in /Users/user1/Entry.php on line 17