cgm225 Posted April 15, 2008 Share Posted April 15, 2008 Sometimes I see people declare all their variables, some people only a few.. My question is simply, what variables need to be declared first in a class? Quote Link to comment https://forums.phpfreaks.com/topic/101258-what-variables-need-to-be-declared-first-in-a-class/ Share on other sites More sharing options...
craygo Posted April 15, 2008 Share Posted April 15, 2008 A class can be a whole ton of functions. You only really need to declare the ones that are core to the function of the class and others as you need them. Ray Quote Link to comment https://forums.phpfreaks.com/topic/101258-what-variables-need-to-be-declared-first-in-a-class/#findComment-517944 Share on other sites More sharing options...
rhodesa Posted April 15, 2008 Share Posted April 15, 2008 First let me say: Variables should always be declared at the beginning of the class But, variables will be created on the fly if you are using them from within the the class. They will be PUBLIC. If you call a variable from outside the class (like $obj->varname) and varname has not been declared (either properly or on the fly) you will get an error. Quote Link to comment https://forums.phpfreaks.com/topic/101258-what-variables-need-to-be-declared-first-in-a-class/#findComment-517950 Share on other sites More sharing options...
KevinM1 Posted April 15, 2008 Share Posted April 15, 2008 Sometimes I see people declare all their variables, some people only a few.. My question is simply, what variables need to be declared first in a class? This question is probably best answered by understanding the very basics of OOP. Classes define the characteristics of objects. They do this by providing (typically) a series of properties (variables) that define the attributes of the object, and methods (functions) that define the the behavior of the object. The class properties depend on what you want the object to represent and do. So, a database object won't have the same properties as a form validation object because they're two wildly different things, with different jobs, despite them both being objects. It all boils down to what you want the objects to do. Quote Link to comment https://forums.phpfreaks.com/topic/101258-what-variables-need-to-be-declared-first-in-a-class/#findComment-517951 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.