Class Variables
Moderator: General Moderators
Class Variables
When should a variable be declared as class property? I would think when it's used by more than one method of the class (including constructor). Is that right, or are there any other reasons?
-
The Monkey
- Forum Contributor
- Posts: 168
- Joined: Tue Mar 09, 2004 9:05 am
- Location: Arkansas, USA
Also if you need to return the variable outside the class.
Unless you have PHP5, however, with the __get() and __set() class methods, the best practice would probably be to return the variable via a method (result() or such). Then, later on, if you need to make sure that some action has occurred before something outside the class instance gets ahold of the variable, you can add the check to the result() method and not change any code outside the class.
Unless you have PHP5, however, with the __get() and __set() class methods, the best practice would probably be to return the variable via a method (result() or such). Then, later on, if you need to make sure that some action has occurred before something outside the class instance gets ahold of the variable, you can add the check to the result() method and not change any code outside the class.