-1

EDIT I've updated the question with actual code. Turns out it was not a scope issue but a stupid mistake on my part. While testing that all value were good I was really setting them to empty.

After reading the answer below I realized I have the scope figured out but had a typo in the code.

Sorry

<?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;
        }
    }
}
dmgd
  • 425
  • 4
  • 15

1 Answers1

2

There is something wrong from beginning, you should write "public function" when you declare a function, not "public functions", and there must be the word "function" declaring a method, not just the name.

Also you are calling a method myfunc1, when it doesn't exists and you have made another mistake when you call func2 (you wrote fucn2).

So, if you fix your code, it works as you want.

Here I fixed it for you:

<?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();

?>

So please be careful before asking and if you can/want use an ide like netbeans for php to avoid this mistakes.

Have a good night.

Juan Sánchez
  • 1,014
  • 2
  • 15
  • 29
  • **Thanks** BTW I do use an IDE Sublime Text. I did learn a lesson "Don't ask questions after a long frustrating day". Thanks again for your response. – dmgd Sep 20 '13 at 14:36