1
我寫了一個小測試場景,用於測試混合使用靜態和非靜態方法。 將公共方法作爲靜態方法調用時,會生成嚴格的警告。 這讓我想,也許調用靜態方法作爲公共方法會產生類似的警告,但它沒有。靜態方法可以作爲公共方法調用嗎?
可以將靜態方法作爲非靜態方法調用嗎?這樣做會有什麼後果嗎?
<?php
class TestS {
static function _testS() {
echo("Hello Static<br>\n");
// Strict warning: Non-static method TestS::_tS() should not be
// called statically in TestS::_tS()
self::_tS();
}
public function _tS() {
echo("tS<br>\n");
}
public function _testP() {
echo("Hello Public<br>\n");
self::_tP();
}
static function _tP() {
echo("tP<br>\n");
}
public function _testSP() {
$this->_tP();
}
}
$test = new TestS();
echo("///////////<br>\n");
$test->_testP();
echo("///////////<br>\n");
TestS::_testS();
echo("///////////<br>\n");
$test->_testSP();
echo("///////////<br>\n");
請不要使用靜態方法。 –
除了首選項之外,是否有任何強制性的參數不使用靜態方法? 我喜歡使用靜態方法/類來保持全局範圍清理大量小型實用功能。 – forsvunnet
靜態方法和變量**的問題是**它們是全局的。 –