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 650011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:00:24+00:00 2026-05-13T22:00:24+00:00

Say I have a class which represents a person, a variable within that class

  • 0

Say I have a class which represents a person, a variable within that class would be $name.

Previously, In my scripts I would create an instance of the object then set the name by just using:

$object->name = "x";

However, I was told this was not best practice? That I should have a function set_name() or something similar like this:

function set_name($name)
{
    $this->name=$name;
}

Is this correct?

If in this example I want to insert a new “person” record into the db, how do I pass all the information about the person ie $name, $age, $address, $phone etc to the class in order to insert it, should I do:

function set($data)
{
    $this->name= $data['name'];
    $this->age = $data['age'];
    etc
    etc

}

Then send it an array? Would this be best practice? or could someone please recommend best practice?

  • 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-13T22:00:24+00:00Added an answer on May 13, 2026 at 10:00 pm

    Using explicit getters and setters for properties on the object (like the example you gave for set_name) instead of directly accessing them gives you (among others) the following advantages:

    • You can change the ‘internal’ implementation without having to modify any external calls. This way ‘outside’ code does not need change so often (because you provide a consistent means of access).
    • You provide very explicitly which properties are meant to be used / called from outside the class. This will prove very useful if other people start using your class.

    The above reasons is why this could be considered best practice although it’s not really necessary to do so (and could be considered overkill for some uses ; for example when your object is doing very little ‘processing’ but merely acts as a placeholder for ‘data’).

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

Sidebar

Ask A Question

Stats

  • Questions 323k
  • Answers 323k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I would recommend profiling the function using Rprof or the… May 14, 2026 at 1:09 am
  • Editorial Team
    Editorial Team added an answer You can do something like this: $("#tag-post").click(function(){ $.ajax({ url: '/tags/ajax/post-tag/',… May 14, 2026 at 1:09 am
  • Editorial Team
    Editorial Team added an answer The simple answer could be to just compute the hours… May 14, 2026 at 1:09 am

Related Questions

In my application at the moment I have (as in so many other applications)
I know that this type of question has been asked over and over again,
Let's say I wanted to define an interface which represents a call to a
First post here... I normally develop using PHP and Symfony with Propel and ActionScript
I have the following challenge, and I haven't found a good answer. I am

Trending Tags

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

Top Members

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.