हां, महत्वपूर्ण अंतर यह है कि static
घोषित विधियों को ऑब्जेक्ट-संदर्भ चर, $this
तक पहुंच नहीं है।
इसके अतिरिक्त, ऑब्जेक्ट संदर्भ में नहीं होने पर एक गैर स्थैतिक विधि का आविष्कार E_STRICT
त्रुटि ईवेंट ट्रिगर करेगा। सक्षम होने पर, उस ईवेंट का डिफ़ॉल्ट व्यवहार त्रुटि लॉग (या STDERR) को संदेश आउटपुट करना है, लेकिन यह प्रोग्राम को चलाने जारी रखने की अनुमति देगा।
इसके अलावा, किसी ऑब्जेक्ट संदर्भ में नहीं होने पर $this
को संदर्भित करने का कोई भी प्रयास E_ERROR
ईवेंट ट्रिगर करेगा।
<?php
error_reporting(-1);
//error_reporting(E_ALL);
class DualNature {
public static function fnStatic() {
if (isset($this)) {
// never ever gets here
$myValue = $this->_instanceValue;
} else {
// always gets here
$myValue = self::$_staticValue;
}
return $myValue;
}
public function fnInstance() {
if (isset($this)) {
// gets here on instance (->) reference only
$myValue = $this->_instanceValue;
} else {
// gets here in all other situations
$myValue = self::$_staticValue;
}
return $myValue;
}
public static function fnStaticDeath() {
return $this->_instanceValue;
}
private static $_staticValue = 'no access to $this';
private $_instanceValue = '$this is available';
}
$thing = new DualNature();
echo "==========\n";
printf("DualNature::fnStatic(): \"%s\"\n", DualNature::fnStatic());
echo "==========\n";
printf("\$thing::fnStatic(): \"%s\"\n", $thing::fnStatic());
echo "==========\n";
printf("\$thing->fnStatic(): \"%s\"\n", $thing->fnStatic());
echo "==========\n";
printf("DualNature::fnInstance(): \"%s\"\n", DualNature::fnInstance());
echo "==========\n";
printf("\$thing::fnInstance(): \"%s\"\n", $thing::fnInstance());
echo "==========\n";
printf("\$thing->fnInstance(): \"%s\"\n", $thing->fnInstance());
echo "==========\n";
printf("\$thing->fnStaticDeath(): \"%s\"\n", $thing->fnStaticDeath());
echo "==========\n";
echo "I'M ALIVE!!!\n";
ऊपर के उत्पादन में है:
==========
PHP Strict Standards: Non-static method DualNature::fnInstance() should not be called statically in example.php on line 45
DualNature::fnStatic(): "no access to $this"
==========
$thing::fnStatic(): "no access to $this"
==========
$thing->fnStatic(): "no access to $this"
PHP Strict Standards: Non-static method DualNature::fnInstance() should not be called statically in example.php on line 47
==========
DualNature::fnInstance(): "no access to $this"
==========
$thing::fnInstance(): "no access to $this"
==========
$thing->fnInstance(): "$this is available"
==========
PHP Fatal error: Using $this when not in object context in example.php on line 29
है कि घटना के व्यवहार उत्पादन के लिए त्रुटि लॉग (या STDERR) के लिए एक संदेश और स्थिति 255
साथ कार्यक्रम बाहर निकलने के लिए उदाहरण के लिए है
त्रुटि रिपोर्टिंग स्तर को E_ALL
में बदलना डिफ़ॉल्ट E_STRICT
चेतावनी संदेशों को दबाएगा (ईवेंट अभी भी प्रचारित होगा), लेकिन $this
का अमान्य संदर्भ अभी भी एक घातक त्रुटि का कारण बन जाएगा और प्रोग्राम से बाहर निकल जाएगा।
वाह, यह वास्तव में काम करता है। PHP वास्तव में एक गड़बड़ है। – Gumbo
@ गम्बो: PHP4 बैककंपेट, यदि आप इसे सख्त पसंद करते हैं, तो सख्त त्रुटियों पर अपवाद फेंक दें। – hakre