Jump to content

what variables to declare?


Bramme

Recommended Posts

Okay, this is not really a technical question, but more an issue of semantics (I think).

 

I've been messing around with OO PHP for a few days now, starting a little home brewn application, extending Smarty. Now I've read OO PHP: Concepts, Techniques and Code, which helped me a lot, but I still have a question:

 

Before you build any functions in your class, you declare your variables. Now, which variables should I declare? All of them I'll use in my Class, even though they're only used in one function, or should I rather only declare the one that will be used in multiple functions.

I'm guessing it's the second one, but the book's not entirely clear on the item. Also, apparently, you can't declare a class variable using another variable?

Link to comment
Share on other sites

For that specific issue, you have to see what is the purpose of the class you are creating. The class is an abstraction of the properties of an object in the real life, so the variables that you have to declare are the ones proprietaries of the object or the variables that you are going to represent that object.

 

For example, an user have the properties: id, login, password, email, etc... all this properties (variables) should be declared if you are building a class for users.

 

Of course if you have, let say, a temporal counter variable inside a for loop inside a function, it shouldn't (doesn't) have to be declared. Think about the scope of the variable, if the variable is just going to be used in one function and doesn't represent an important property of the object then it's not necessary, since you just need it for that function.

 

Hope it helps, good luck.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.