Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1025891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:59:53+00:00 2026-05-16T11:59:53+00:00

I was wondering what is better or more accepted when coding in PHP. I

  • 0

I was wondering what is better or more accepted when coding in PHP. I was taught, in Java, that class methods to get and set variables should be prefixed with “get” and “set”. What I want to know, though, is should I use these prefixes on regular PHP functions.

For example, to retrieve a username from a session variable I would either have
getUsername()
or
username()

What are the advantages and best practices. I know that using “get” is more mnemonic but it’s rather redundant (especially for a personal project that I don’t expect to be having other people read) but for the sake of good practice I would like to get it right.

While I’m at it, what is the proper naming convention for variables? Underscore-separated or camel-case? I’ve looked around and I’ve seen a mix of both. WordPress tends to use underscores in their function names but a lot of other sites say camel-case is best.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:59:53+00:00Added an answer on May 16, 2026 at 11:59 am

    I personally try to stay away from getters/setters. I prefer to use the magic methods so then I can do $foo->myVar without needing to explicitly call a function (I think it makes the code more readable).

    With that said, there are circumstances where I use explicit getters and setters (Basically in situations where the result would be ambiguous using the variables directly. Like if I map the magic methods to an internal array and have other member variables that need accessing). In those circumstances, I use $foo->getMyVar() as the function signature.

    IMHO, $foo->myVar() makes sense if you know it’s a variable. But what happens if you see $foo->show(). Does that mean to perform the show action? Or does that mean get the current setting for the show variable?

    I always try to name all methods with something that identifies what they do. $foo->var() doesn’t give any indication to what’s going on. But $foo->showVar() does (At least moreso).

    As for naming convention, it’s all about who you read. I prefer camel-case. But there’s nothing wrong with underscores. Pick one, and stick with it. Consistency is more important than the choice itself…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 499k
  • Answers 500k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is not pretty but it works: rm -R $(ls… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer Yes. Override the base1 and base2 methods in Derived to… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer No, you can't. Unfortunately, UIEvent doesn't expose any public way… May 16, 2026 at 12:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.