2012-10-10 148 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