Page 1 of 1

declaring instance variables

Posted: Fri Oct 22, 2010 11:37 pm
by the_cheat
I can't find anything about this, I just want to know if this is a valid line:

Code: Select all

private $var1, $var2, $var3;

Re: declaring instance variables

Posted: Sat Oct 23, 2010 2:13 am
by cpetercarter
Yes.

Re: declaring instance variables

Posted: Sat Oct 23, 2010 6:13 am
by s.dot
Yes, it is a valid line.

They will be declared but their values will be NULL.. therefore running isset() on them will return FALSE. Interestingly enough, a non-declared variable will have the same value as a declared variable.

check out this code

Code: Select all

<?php

class varTest
{
	private $a, $b, $c;
	
	function isVarSet($var)
	{
		return isset($this->$var);
	}
	
	function otherTest($var)
	{
		var_dump($this->$var);
	}
}

//object
$varTest = new varTest();

//see what's in it
var_dump($varTest);

//check if isset()
var_dump($varTest->isVarSet('a'));
var_dump($varTest->isVarSet('b'));
var_dump($varTest->isVarSet('c'));

//check undeclared variable
$varTest->otherTest('d');
and result
[text]object(varTest)#1 (3) {
["a:private"]=>
NULL
["b:private"]=>
NULL
["c:private"]=>
NULL
}
bool(false)
bool(false)
bool(false)
NULL
[/text]