Page 1 of 1

Variable declaration in classes

Posted: Tue Dec 14, 2004 8:30 pm
by Steveo31
Is it needed? Like so:

Code: Select all

class Member {
    
    //member variables
    var $username;
    var $password;
    var $first;
    var $last;
    var $email;
    var $registerDate; 
    var $interests;
    var $profile;
    var $estore;
    var $pageDir;
    var $memberLevel;
    var $mailingList;
    
    function setVars(){
        //...
    }
}
Or will it work when you do $classVar->setVars($value)..

Posted: Tue Dec 14, 2004 8:56 pm
by rehfeld
class variables work just like regular php variables

$this->foo = 'foo';

if $foo hasnt been declared, it will be created

and if you create it inside a class function, it will still work like as if you had declared it.


but imo its a bad habit not to declare them first. declaring them imo mkaes the class more readable and easier to see whats being done.

espescially if you declare them and set thier initial values or just an empty value to set the type

Code: Select all

class FOO {
    var $foos = array(); // i know $foos will be used as an array now, 
    var $bar = ''; //string


}

Posted: Wed Dec 15, 2004 2:23 am
by Steveo31
Ah ok, cool. Thanks :) Makes great sense.