After all this time doing PHP it never occured to me that the following is perfectly valid:
Code:CLASS testClass{ public $attribute; public $attribute2=10; function testClass($a){ $this->undeclared=$a; } function flag(){ $this->flag=true; } function out(){ echo $this->undeclared=$a; } } $b=new testClass(5); $b->out;
This is just as valid tho extremely more risky if you dont keep track of your own code...
Code:CLASS testClass{ public $attribute; // this is what I am referring to when I say "declaring" public $attribute2=10; // this is what I am referring to when I say "declaring" function out(){ echo $this->undeclared=$a; } } $b=new testClass(); $b->undeclared=5 $b->out;
My question is... other than "organizational" value, what is the real point/benefit to declaring public properties when creating a class when, essentially, you can create them at any point in the code, in same way you would create regular variables (except with $instanceName-> or $this-> preceding the variable). It would seem that there are even some uses to NOT declaring public variables at times. Example , you can tell if the flag function has been called at least once in any particular instance with isset($this->flag).
Am I some how way of the mark here? I would love to hear the opinion of he forum experts on this topic.





Reply With Quote






Bookmarks