Why is it in a class, so important for you to initialize your $variables?

In the following example, I have my class started and then took out or
atleast commented my "var $variable" and the code still does what it should.
So why is it so important that you initialize these variables this way?

[:::CODE:::]
<?php
class test {

        //var $z;               <------This is the part I don't see
                                the relevance in doing as here this is
                                commented out and the class works as expected.

        function z(){
                $this->z = 250;
                return $this->z;
        }

        function test1($a,$b){
                $this->tested = $a + $b;
                return $this->tested;
        }

        function test2($c,$d,$e,$f){
                $this->test2 = $c + $d;
                $this->test3 = $this->test1($e,$f) + $this->test2 + $this->z();
                return $this->test3;
        }

}
$whynot = new test();
echo $whynot->test2(10,10,10,20);
?>
[:::/CODE:::]

I don't have any real use for this code to work after this point, I'm just
trying to generate somesort of script that makes sense to me. As my previous
attempt at jumping feet first into classes failed miserably.

I guess I'm just confused on why you would want to initialize a variable and
not assign a value to it if you know what it is? Like in my Database class,
I made my username, pass, localhost, etc variables initiated with their
values, but kept getting errors from mysql that I didn't have a "Valid"
mysql resource. I guess after reading this in 4 books and on php.net, they
all do this but none of them go into WHY it's so important.

Part of my concern is that I'm being told, or atleast reading that you
initialize them for the class, but if the class structure is already in
place for the variables inside each method to be local to that class, I
don't see the relevance for initialization. Man this sounds dumb cuz I just
know there's a reason, I just can't find it. Any help would be appreciated.
Thanks.

Wolf

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to