-1

编辑我已经用实际代码更新了这个问题。事实证明这不是范围问题,而是我的一个愚蠢错误。在测试所有值都很好时,我真的将它们设置为空。

阅读下面的答案后,我意识到我已经弄清楚了范围,但代码中有错字。

对不起

<?php

abstract class PHPFoo_XYZ
{

    protected $_postData   = array();

    public function processXYZ(array $postData)
    {
        $this->_postData = $postData;
    }

    protected function _checkProcessId()
    {
        // doing nothing
    }

}
?>



<?php

require_once dirname(__FILE__) . '/../PHPFoo/XYZ.php';

class App_XYZ extends PHPFoo_XYZ
{

    protected $_UserData = array();
    protected $_UserId = 'notset';
    protected $_UserName = '';
    public $_msg = '';


    public function processXYZ(array $postData)
    {
        $this->_postData = $postData;

        $this->_getUserData();

        $this->_checkProcessId();
    }


    protected function _checkProcessId()
    {
        $this->_writeLog("User Name ".$this->_UserName);
        $this->_writeLog("User Id ".$this->_UserId);
    // These show empty 
    }

    public function _getUserData() {

        $UserData = array();
        $UserId = array();
        $User_Name = array();
        $msg = '';

        // Get data from database
        $this->_UserId = $UserId[0]['item_id'];
        // Get data from database
        $this->_UserName = $User_Name[0]['title'];
        // Get full data

        // $results = Array of values from database

        foreach ($results as $key => $value) {
            $UserData[$results[$key]['fielddef_id']] = $results[$key]['value'];
        }
        $this->_UserData = $UserData;
        $this->_writeLog("USER DATA FULL");
        $this->_writeLog("User Name ".$this->_UserName);
        $this->_writeLog("User Id ".$this->_UserId);
        $msg = '';
        foreach ($this->_UserData  as $k => $v) {
             $msg .= "\n".$k." == ".$v;   
        }
        $this->_writeLog("User Data\n".$msg);

        // The above output is good

        if($this->_UserData = '' || $this->_UserId = '' || $his->_UserName = '') {
            $this->_writeLog("There was an error getting User Data.");
            return false;
        }else{
            return true;
        }
    }
}
4

1 回答 1

2

从一开始就有问题,声明函数时应该写“public function”,而不是“public functions”,而且声明方法必须有“function”二字,而不仅仅是名称。

此外,您正在调用一个方法 myfunc1,但它不存在并且您在调用 func2 时犯了另一个错误(您编写了 fucn2)。

所以,如果你修复你的代码,它会按你的意愿工作。

在这里,我为您修复了它:

<?php

abstract class foo {

    protected $_var1 = '';
    protected $_var2 = '';

    public function func1() {
        #code...
    }

    public function func2() {
        #code..
    }

}

class bar extends foo {

    protected $myvar1 = '';
    protected $myvar2 = '';

    public function myfunc() {
        // do some code to fill myvar1 to be used in other functions
        $this->myvar1 = 'some data';
        echo "my var " . $this->myvar1;
    }

    public function func2() {
        // do some code that uses myvar1 data
        // but $this->myvarf1 is empty here why?
        echo $this->myvar1;
    }

    public function runit() {
        $this->myfunc();
        $this->func2();
    }

}
//requre file
$callclass = new bar;
$callclass->runit();

?>

所以在询问之前请小心,如果你可以/想要使用像 netbeans 这样的 ide 来避免这个错误。

祝你晚安。

于 2013-09-19T03:15:55.723 回答