-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 functions」ではなく「public 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();

?>

したがって、質問する前に注意してください。また、この間違いを避けるために、php に netbeans のような ide を使用できる/使用したい場合は注意してください。

良い夜を。

于 2013-09-19T03:15:55.723 に答える