[PHP] OOP variables
Hello, I was wondering if somebody would mind explaining this to me, is there a big difference or requirement to pre defining variables your going to use in a class eg: class name { $var1 = ''; $var2 = ''; function blah(){ $var2 } }//-- End class Or is this also right class name { function name(){ $this-var1 = ''; $this-var2 = ''; } function blah(){ } }//-- End class Thanks Jarratt -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] OOP variables
http://www.php.net/manual/en/language.oop.php states: [quote] In PHP 4, only constant initializers for var variables are allowed. To initialize variables with non-constant values, you need an initialization function which is called automatically when an object is being constructed from the class. Such a function is called a constructor (see below). [/quote] Richard PS: Isn't it cool when every explanation is already written down in a manual? Thursday, April 15, 2004, 10:27:00 AM, you wrote: Hello, I was wondering if somebody would mind explaining this to me, is there a big difference or requirement to pre defining variables your going to use in a class eg: class name { $var1 = ''; $var2 = ''; function blah(){ $var2 } }//-- End class Or is this also right class name { function name(){ $this-var1 = ''; $this-var2 = ''; } function blah(){ } }//-- End class Thanks Jarratt -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] OOP variables
[quote] In PHP 4, only constant initializers for var variables are allowed. To initialize variables with non-constant values, you need an initialization function which is called automatically when an object is being constructed from the class. Such a function is called a constructor (see below). [/quote] Another thing to consider is whether your class needs to or could be accessed statically. If it can then whatever variables you might need in executing the class methods can't be solely defined in the constructor. When used statically, the class' constructor is (obviously) never run. Chris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php