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

  • SEARCH
  • Home
  • 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 7970493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:24:29+00:00 2026-06-04T07:24:29+00:00

I have seen some some projects in which classes are having get and set

  • 0

I have seen some some projects in which classes are having get and set methods to manipulate insert data. Let me have an example here :

    class Student extends dbClass
{
    private $TableID;
    private $FullName;
    private $Gender;
    private $Address;




    function setTableID($Value)
    {
        $this->TableID = $Value;
    }

    function getTableID()
    {
        return $this->TableID;
    }

    function setFullName($Value)
    {
        $this->FullName = $Value;
    }

    function getFullName()
    {
        return $this->FullName;
    }

    function setGender($Value)
    {
        $this->Gender = $Value;
    }

    function getGender()
    {
        return $this->Gender;
    }

    function setAddress($Value)
    {
        $this->Address = $Value;
    }

    function getAddress()
    {
        return $this->Address;
    }


    function UpdateStudent()
    {
        $sql = "UPDATE INTO usertable SET
        FullName = '".$this->getFullName()."',
        Gender = '".$this->getGender()."',
        Address = '".$this->getAddress()."'
        where TableID='".$this->getTableID()."'";
        $this->query($sql);
    }
}

Above is the example class that i have seen. And below is the process how they are using it :

$student = new Student;
$student->setTableID = 1;
$student->setFullName('My Name');
$student->setGender('Male');
$student->setAddress('this is my address');

$studen->UpdateStudent();

Is it worth doing this way? I personally think its useless to set field and then get and update records in it. It really takes a lot of time to make it for every module. What is the best way to handle such thing?
Is there any security concerned doing it in this way?

  • 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-06-04T07:24:30+00:00Added an answer on June 4, 2026 at 7:24 am

    Is it worth doing this way?

    It depends.

    Abstracting a field from the user by exposing a “smart” property (i.e. getter and/or setter) has two disadvantages:

    1. You need to write more code; if the property doesn’t really do anything smart, this is code that does nothing useful.
    2. The user of the property is slightly inconvenienced because they have to type a little more as well.

    And it has one advantage:

    1. In the future you can add logic to the properties even if there was none before without breaking your users’ code.

    If this advantage is meaningful (e.g. you are writing a reusable software library) then it makes great sense to write properties instead of bare fields. If not, you are doing work for no benefit.

    What is the best way to handle such thing?

    You can override the magic __get and __set functions (perhaps in a base class so you can inherit the override as well) to automatically forward property accesses to your getters and setters. Simplified code:

    public function __get($name) {
        $getter = 'get'.$name;
        if (method_exists($this, $getter)) {
            return $this->$getter();
        }
    
        $message = sprintf('Class "%1$s" does not have a property named "%2$s" or a method named "%3$s".', get_class($this), $name, $getter);
        throw new \OutOfRangeException($message);
    }
    
    public function __set($name, $value) {
        $setter = 'set'.$name;
        if (method_exists($this, $setter)) {
            return $this->$setter($value);
        }
    
        $getter = 'get'.$name;
        if (method_exists($this, $getter)) {
            $message = sprintf('Implicit property "%2$s" of class "%1$s" cannot be set because it is read-only.', get_class($this), $name);
        }
        else {
            $message = sprintf('Class "%1$s" does not have a property named "%2$s" or a method named "%3$s".', get_class($this), $name, $setter);
        }
        throw new \OutOfRangeException($message);
    }
    

    Caveat emptor: Since __get and __set are overridden, __isset and __unset should be overridden as well!

    Is there any security concerned doing it in this way?

    No, none at all (assuming you don’t insert bugs accidentally).

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

Sidebar

Related Questions

I have seen some mails which has HTML content embedded in them. The content
I have seen that some developers have a graphical representation of all their classes
During my apprenticeship, I have used NHibernate for some smaller projects which I mostly
Have seen some similar questions: What is the difference between a JavaBean and a
I have seen some instances where people will say you have to use JS
i have seen some hand rolled solutions but does jquery out of the box
I have seen some developers use the return statement in a catch block. Why/when
I have seen some of the other answers on this topic but dont really
I have seen some apps with a button to rate the app. It brigs
I have seen some demos about the jQuery wizard plugin on websites. But all

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.