Undefined variable error with protected variable (solved)


#1

Hi Folks, the variable $_monthNumber is defined protected in the class und used in the functions initialize and guvpdf.
I get the error: undefined variable in guvpdf, bute why??
As I understood protected variables are visible in the same class. I have used it with $this and without-

class JournalsController extends AppController {

protected $array;
protected $product;
protected $_monthNumber;

public function initialize() {
    parent::initialize();
    $this->set('menuID', 'accounts');
    $this->set('product', 'product3');
    $this->loadComponent('accountUpdate');
    $this->loadComponent('divTools');
    $timestamp = time();
    $_monthNumber = date("n", $timestamp);


}  

public function guvpdf() {
$actYear = Configure::read(‘actualYear’);
$months = Configure::read(‘Months’);
$orient = ‘portrait’;
.
.
$this->set(‘month_number’, $this->$_monthNumber);
$this->set(‘month_names’, $months);
}


#2

$this->set(‘month_number’, $this->_monthNumber);


#3

Hi, thanks this is helping to avoid the error, and I never read about removing the leading $-sign.
Now the error is gone, but the variable is empty. In the ‘initialize’ the value is available but not in another function of the same class. Do you know why?


#4

should be

$this->_monthNumber = date("n", $timestamp);

http://php.net/manual/en/language.oop5.properties.php


#5

Thanks, with some more readings and your advice now I understand the system and can apply it throughout the application.