我搜索过,发现了这个问题,这对我有所帮助: 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()时错过了括号。但是,我现在有另一个问题。我似乎无法将classCode从类中取出(稍后在另一个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()中的代码,CORRECT值就会打印出来!但是如果我尝试从getResultCode()之后访问它,它会再次返回NULL。发生了什么事?
答案 0 :(得分:2)
echo 'I just set it to: ' . $this->getResultCode;
我认为你在这里缺少一些括号。
答案 1 :(得分:0)
getResultCode()
答案 2 :(得分:0)
使用像这样的回报
public function getResultCode() {
return self::$result['code'];
}
并使用$this->getResultCode();
修改强>
我能看到的唯一问题是你刚刚写了return;
,因为它返回NULL
,将其更改为
return $this->getResultCode();
答案 3 :(得分:0)
您必须将getResultCode()作为方法调用。
echo 'I just set it to: ' . $this->getResultCode();
答案 4 :(得分:0)
一个。你的代码是错误的......在PHP中没有像__constructor
这样的东西
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