![]() |
Declare Members at top of Class or Just Before Use? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Declare Members at top of Class or Just Before Use? (/showthread.php?tid=34729) Pages:
1
2
|
Declare Members at top of Class or Just Before Use? - El Forum - 10-07-2010 [eluser]Bleeding Edge Productions[/eluser] Hi guys, Not that it makes the slightest bit of difference to the execution of the script, but is it better practice to declare members (particularly variables) at the top of a class, or just before they are used? Either: Code: <?php or: Code: <?php (obviously this is a minimal example!) Cheers, Martin Declare Members at top of Class or Just Before Use? - El Forum - 10-07-2010 [eluser]techgnome[/eluser] It's probably a matter of preference... but when it comes to PHP, I tend to declare everything at the top. Especially if others are going to use it. Seems to me, that's typically where most people will look first. -tg Declare Members at top of Class or Just Before Use? - El Forum - 10-07-2010 [eluser]Bleeding Edge Productions[/eluser] Thanks for the reply. That seems to be a pretty common way of doing it. Do you comment the declarations (as to where they are used in the class / method)? Declare Members at top of Class or Just Before Use? - El Forum - 10-08-2010 [eluser]n0xie[/eluser] If you declare them at the top, you get the added bonus that your IDE will pick them up and will help you when using them. Declare Members at top of Class or Just Before Use? - El Forum - 10-08-2010 [eluser]techgnome[/eluser] BEP - yes and no... depends on the situation... some of the more complex ones I do... like the definitions of my db record arrays... mostly about what fields I've got loaded at that point, prevents me from trying to access a field I didn't load. I do development professionally (just not PHP) so my philo is to try to name things what they are so that code becomes almost self documenting as much as it can. I don't use junk variables like $i unless I need a quick one for a loop where it doesn't mean much anyways (altho, I'll usually default to $counter). n0xie - blah! That's cheating! Notepad... that's where the power is at! ![]() -tg Declare Members at top of Class or Just Before Use? - El Forum - 10-08-2010 [eluser]OliverHR[/eluser] Bonus!!????, matter of preference????, This is all about SCOPE! and memory usage. http://en.wikipedia.org/wiki/Scope_(programming) Declare Members at top of Class or Just Before Use? - El Forum - 10-08-2010 [eluser]techgnome[/eluser] Given the example where the second set of variables were declared between two functions, scope is irrelevant. If var 3 & 4 were declare inside the function, then scope is a factor. In this contrived example though, all four vars are at the same level (class) and so have the same scope. Now, what I don't remember off the top of my head is if in the "split" case - example 2 - if $var3 and $var4 can be used in someFunciton1 ... something tells me that they should since they are scopped at the class level... which means that the answer is "probably not"... which would be a reason to declare all your vars at the top. -tg Declare Members at top of Class or Just Before Use? - El Forum - 10-09-2010 [eluser]InsiteFX[/eluser] techgnome is correct! Variables declared at the Class level are global to that Class! Variables declared at the method/function level are only avaiable to that method/function. Unless you use global variables. InsiteFX Declare Members at top of Class or Just Before Use? - El Forum - 10-09-2010 [eluser]OliverHR[/eluser] The decision to declare variables should not be taken for an ide "bonus", also if any of these variables will be used only(exclusively) within a method, I'm not see why declare them as class properties because this only increases memory usage. Declare Members at top of Class or Just Before Use? - El Forum - 10-09-2010 [eluser]n0xie[/eluser] [quote author="OliverHR" date="1286663679"]The decision to declare variables should not be taken for an ide "bonus", also if any of these variables will be used only(exclusively) within a method, I'm not see why declare them as class properties because this only increases memory usage.[/quote] Variables are used inside a method and are locally scoped to that method. Hence they are named differently. One usually doesn't 'declare' variables. The question specifically targeted class properties. By definition these are used throughout your class since they are a member of your class. The fact that they are globally scoped inside your class is probably what you want. The right way to do it, is to declare them at the top of your class so that whenever another developer looks at your class, he will know what properties he has access to. Declaring them per method is just confusing. |