2012-10-10 151 views
0

我搜索,發現這個問題,這讓我: php static variable is not getting set靜態變量未設置?

它沒有,但是,沒有解決不了我的整個問題。

代碼:

Class DummyClass { 
    public static $result; 

    function __construct() { 
     $this->_setResultCode('testing'); 
    } 

    public function getResultCode() { 
     return self::$result['code']; 
    } 

    private function _setResultCode($val) { 
     echo 'Im gonna set it to: ' . $val . '<br />'; 
     self::$result['code'] = $val; 
     echo 'I just set it to: ' . $this->getResultCode; 
     die(); 
    } 
} 

輸出:

Im gonna set it to: testing 
I just set it to: 

這是怎麼回事嗎?這怎麼可能?


編輯:問題是調用getResultCode當我錯過了括號()。不過,我現在有另一個問題。我似乎無法將resultCode從類中取出(稍後在DummyClass的另一個實例中)。

這裏是我的相關編碼(沒有更多的示例代碼,因爲我似乎一團糟,最多):

Class lightweightContactFormPlugin { 

// Set up/Init static $result variable 
public static $result; 

function __construct() { 
    //echo 'I just inited<br/><pre>'; 
    //var_dump($this->getResultCode()); 
    //echo '</pre><br/>'; 
} 

public function run() { 

    // Set default value for resultCode 
    $this->_setResultCode('no_identifier'); 

    // Check if form was posted 
    if(isset($_POST['cfidentifier'])) { 
     $fields = $this->_get_fields_to_send(); 
     $valid = $this->_validate_fields($fields); 

     // Only continue if validatation was successful 
     if($valid == true) { 
      // Store mail result in $mail 
      $mail = $this->_send_mail($fields); 

      // Yay, success! 
      if($mail) { 
       $this->_setResultCode('sent_successfully'); 
       return; 
      } else { 
       // Couldn't send mail, bu-hu! 
       $this->_setResultCode('not_sent'); 
       return; 
      } 
     } 
     $this->_setResultCode('validation_fail'); 
     return; 
    } 
} 


    // Get and Set methods 
public function getResultCode() { 
    return isset(self::$result['code']) ? self::$result['code'] : ''; 
} 

private function _setResultCode($val) { 
    self::$result['code'] = $val; 
} 
} 

留下了一些不相關的方法了。沒有其他方法設置或獲取resultCode,它應該沒關係。

任何想法,爲什麼我不能訪問對象的另一個實例中的$ result ['code']?

我這樣做,當我訪問它:

$plugin = new lightweightContactFormPlugin(); 
    $cfstat = $plugin->getResultCode(); 
    echo '<pre>'; 
    var_dump($fstat); 
    echo '</pre>'; 

結果是:

NULL 

奇怪的是,如果我在__construct()取消註釋代碼,正確的值不會獲取打印出來了!但是,如果我嘗試從getResultCode()之後訪問它,它將再次返回NULL。到底是怎麼回事?

+0

yourt getResultCode函數不返回任何東西。 – Najzero

+0

這是代碼中的一個錯誤,我剛剛注意到了。這不是問題,我已經設置它返回self :: $ result ['code']'它應該是。我現在解決了問題中的代碼。 – qwerty

+0

您正在傾銷'$ fstat',但正在將'getResultCode'的返回值寫入名爲'$ cfstat'的變量。 –

回答

2
echo 'I just set it to: ' . $this->getResultCode; 

不歸我想你在這裏失去了一些括號。

+0

榮譽給你我的好人! – qwerty

+0

@qwerty你會驗證答案,也許=)? – blue112

+0

我試過了,不得不等幾分鐘。 – qwerty

0

你有內getResultCode()

+0

這是一個錯誤,我剛剛編輯了這個問題。 blue112有答案。 – qwerty

+0

你能看看我的編輯嗎?仍然有問題。 – qwerty

0

使用return這樣

public function getResultCode() { 
    return self::$result['code']; 
} 

,並使用$this->getResultCode();

編輯

唯一的問題我可以看到的是你剛纔寫return;,因爲它返回NULL,變化它到

return $this->getResultCode(); 
+0

這是一個錯誤,我只是編輯了這個問題。 blue112有答案。 – qwerty

+0

你可以看看我的編輯嗎?仍然有問題。 – qwerty

+0

@qwerty,但你說你的問題已經解決了。 –

0

您必須調用getResultCode()作爲方法。 A.你的代碼是錯誤的... ...

echo 'I just set it to: ' . $this->getResultCode(); 
+0

請你看看我的編輯?仍然有問題。 – qwerty

0

有沒有像__constructor在PHP它應該是

 function __construct() { 

B.你的代碼也應該返回以下自getResultCode沒有設置

Notice: Undefined property: DummyClass::$getResultCode 

你應該叫

echo 'I just set it to: ' . $this->getResultCode(); 

你最終代碼:

class DummyClass { 
    public static $result; 

    function __construct() { 
     $this->_setResultCode('testing'); 
    } 

    public function getResultCode() { 
     return self::$result['code']; 
    } 

    private function _setResultCode($val) { 
     echo 'Im gonna set it to: ' . $val . '<br />'; 
     self::$result['code'] = $val; 
     echo 'I just set it to: ' . $this->getResultCode(); 
     die(); 
    } 
} 

new DummyClass(); 

輸出

Im gonna set it to: testing 
I just set it to: testing 
+0

A點是一個錯字。我沒有複製我的課程代碼,我寫了一個虛擬課程,只是複製了getResultCode()和_setResultCode()方法(幾乎沒有改動)。 B點是有效的,但我忘了括號。 – qwerty

+0

你可以看看我的編輯嗎?仍然有問題。 – qwerty

+0

@qwerty從你的代碼中調用'$ plugin-> run();'所以沒有輸出被返回......參見:http://codepad.viper-7.com/4VuifJ – Baba