Forum Moderators: coopster

Message Too Old, No Replies

OOP - why do you have to declare vars?

         

jamie

1:56 pm on May 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi,

i am a bit confused.

in a couple of classes i have done for setting up an online shop, i can delete the var declarations in the class and it still functions as normal?

what is the point of declaring vars - and which vars should be declared?

thanks

jatar_k

3:13 pm on May 1, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



take a look how your error reporting is set and change it to error_reporting(E_ALL) and then try both ways and see what notices you get

Lord Majestic

3:15 pm on May 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



what is the point of declaring vars - and which vars should be declared?

Declaration allows compiler to warn you when you have accidentally created new variable with same name as the other in the same scope, and further operations will be done with local var rather than global one.

This sort of errors are hard to spot because logic looks just right, but you can't see other declarations that were made outside of the function where as the compiler has global world view of your code.

jamie

10:23 pm on May 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



thanks both,

jatar_k - somehow i had managed to turn off reporting on my dev server (i deleted the php.ini by mistake)

it is now on and i am getting all sorts of little errors. am just sorting these out now.

cheers

Stormfx

3:45 am on May 4, 2005 (gmt 0)

10+ Year Member



Just a note about class variables. You can't call an undefined variable, but you don't have to define them before assigning them a value, either.

For example:

class test {
function var($var) {
$this->var = $var;
return $this->var;
}
}

Is just as legal as:

class test {
var $var = '';
function var($var) {
$this->var = $var;
return $this->var;
}
}

However I myself like to pre-define the vars ahead of time just so I know what all is there.

ergophobe

5:22 pm on May 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Both of those are legal, true, but there's no real point in those examples to assigning the value to an object property instead of a local variable, since the property is only available in local scope. So the following are both legal, but the results will be different.


class class1
{
var $var1=7;

function func1()
{
$this->var1 = (isset($this->var1))? $this->var1 + 1 : 1;
return $this->var1;
}
}


class class2
{
function func2()
{
$this->var2 = (isset($this->var2))? $this->var2 + 1 : 1;
return $this->var2;
}
}

$class1 = new class1();
$class2 = new class2();

echo $class1->func1(); // returns "8"
echo $class1->var1; // returns "8"

echo $class2->func2(); // returns "1"
echo $class2->var2; // returns warning - undefined

In other words, class2 could be rewritten as


class class2
{
function func2()
{
$var2 = (isset($var2))? $var2 + 1 : 1;
return $var2;
}
}

Stormfx

6:18 pm on May 4, 2005 (gmt 0)

10+ Year Member



Oh, you're right. I've no idea why that slipped my mind. I'm so used to keeping the members in the class and using a function to return a value, heh. My apologies! :)

StupidScript

9:31 pm on May 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



i can delete the var declarations in the class and it still functions as normal

If the variable is only being used locally within the class, then that's normal, but if you are using the same variables globally, isn't the above an indicator that

register_globals
is ON?

ergophobe

11:20 pm on May 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



register_globals only affects GET, POST, etc data. It doesn't auto global everything.

It probably means that you simply aren't using the object properties outside their scope. In other words, you aren't using a function to set

$this->prop = 6;

in a class function and then trying to access it outside the function as

$var = $obj->prop;


If the variable is only being used locally within the class, then that's normal,

Actually, it's a bit more specific than that. What I said above applies within a class as well. If you do not have a var declaration and you use that var in two different class functions, that info won't be shared.

Let's assume that you have error reporting turned down so it doesn't stop at the undefined variable.

class classy
{
function func1 {
$this->myvar += 7;
echo $myvar;
}

function func2 {
$this->myvar += 7;
echo $myvar
}
}

$obj = new classy();

$obj->func1(); // outputs "7" regardless of var declaration
$obj->func2(); // outputs "7" if no var declaration; "14" if there is one

StupidScript

11:37 pm on May 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Aah. Thanks, ergophobe!

So in your classy example, declaring $myvar outside of the class (preceding it's functions invocation) will turn $myvar into a "global" variable, and not declaring it (like the example) renders it a "local" (to each function separately within the class) variable?

And in either case, having register_globals turned on will have no effect on that example?