【问题标题】:Need simple php oop scope matrix需要简单的 php oop 范围矩阵
【发布时间】:2013-09-18 23:23:06
【问题描述】:

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

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

对不起

<?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;
        }
    }
}

【问题讨论】:

    标签: php oop scope php-5.4


    【解决方案1】:

    一开始就有问题,声明函数的时候应该写“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 来避免这个错误。

    祝你晚安。

    【讨论】:

    • 谢谢顺便说一句,我确实使用了 IDE Sublime Text。我确实学到了一课“在漫长的令人沮丧的一天之后不要问问题”。再次感谢您的回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-30
    • 1970-01-01
    • 2017-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多