Jump to content

Good programming practice


paul2010

Recommended Posts

Just a few questions about good programming practice in PHP coming from a Java background.

 

How important are getter and setter functions or is it acceptable to use to just have a variable set to public?

 

what is the scope of a variable inside  a private a function?

 

is it best to declare all variables as private and when using them inside a function use $this->name ?

 

Thanks

Link to comment
Share on other sites

How important are getter and setter functions or is it acceptable to use to just have a variable set to public?

 

In any language it's better to provide a clear interface using methods rather than properties in my opinion.

 

what is the scope of a variable inside  a private a function?

 

Variables created within a function (or a method) are only available within said function.

 

is it best to declare all variables as private and when using them inside a function use $this->name ?

 

Often it is best to keep the inner workings of a class private. If you don't need access to the variable from outside of an object, it should be private. Even then, I would suggest still keeping variables private and creating getter setters.

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.