At 3:00 PM -0600 1/25/01, Matt wrote:
>I have a question that may seem kind of silly, but I'm curious...
>
>When using PHP why would one use "var" to define a variable as 
>opposed to just regularly creating it?


Because that's the way it is ;).

The var is part of the syntax of a class definition; it isn't used 
anywhere else. I don't know the actual deep reason for having it, as 
far as the parser is concerned, but it does make it clear - at least 
to me - what the class variables are.

You can also initialize the variable here, too:

        var $a = 5;


>For example, if I have a class defined as below:
><pre>
>class Simple {
>   var $a;
>
>   function Simple() {
>     $this->a = 5;
>   }
>
>   function first() {
>     return $this->a;
>   }
>
>}
></pre>
>
>This class, when created, sets the variable $a to 5, and the 
>function first returns the value in $a.... my question is could I 
>omit the var and still have it function in this fashion? What is the 
>purpose of it?
>
>Also, can I access this variable through the same -> convention, 
>i.e. would the following be allowed?
><pre>
>$test = new Simple();
>echo $test->a;
></pre>
>

Yep.

You can also _set_ $a this way:

        $test = new simple;
        $test->a = 77;
        echo $test->first();

will display 77.

See
        http://www.php.net/manual/en/language.oop.php

for more info.

        - steve

-- 
+--- "They've got a cherry pie there, that'll kill ya" ------------------+
| Steve Edberg                           University of California, Davis |
| [EMAIL PROTECTED]                               Computer Consultant |
| http://aesric.ucdavis.edu/                  http://pgfsun.ucdavis.edu/ |
+-------------------------------------- FBI Special Agent Dale Cooper ---+

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to