Page 1 of 1

Classes and Initializing Variables

Posted: Tue Nov 22, 2011 5:25 am
by Sindarin
Is it generally good practice to do:

Code: Select all

class MyClass{

	public $testvar;
	
	public function __construct(){
		$this->testvar = 1;
	}

}
instead of:

Code: Select all

class MyClass{

	public function __construct(){
		$this->testvar = 1;
	}

}
or is it redundant?

Re: Classes and Initializing Variables

Posted: Tue Nov 22, 2011 5:37 am
by maxx99
In second case $testvar is just undeclared. In PHP you don't have to declare your class variables, but its a good practice:
- you have a clear list of your variables
- you can set visibility (public/private)
- you can add PHPDOC description of this variable

Re: Classes and Initializing Variables

Posted: Wed Nov 23, 2011 12:44 am
by social_experiment
http://framework.zend.com/manual/en/cod ... style.html
Look at the 'classes' section of this page for more information