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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:02:51+00:00 2026-05-22T22:02:51+00:00

in C#, getters and setters can be defined and filled differently for each attribute

  • 0

in C#, getters and setters can be defined and filled differently for each attribute (property) but in php it looks a common gateway for all the attributes. Is there is a way to define getters and setters for each properties like the code shown below in C#:

private string hello;
private string world;

public string Hello
{
    get{ return hello;}
    set{ hello = value + "this one is hello";
}

public string World
{
    get{return world;}
    set{world = value + "this one is world";
}

What I want is a similar way to define different getters and setters for some particular attributes.

Edit:

I know a way but they needs function calls like set_attr1($value){} and get_attr1(){} and declare private $attr1 but as you may see, it is not quite what I want. I want them invoked automatically when I reach $attr1

Thanks.

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

    You could set a magic method with a switch case, but that won’t fix your issue at 100%, because the magic method will not get invoked unless the property does not exist or if its scope is not accessible from the caller:

    class Foo {
       private $bar = 'hello';
       private $baz = 0;
    
       public function __set($var, $value) {
           switch ($var) {
               case 'bar':
                   $this->$var .= $value;
                   break;
               case 'baz':
                   $this->$var += $value;
                   break;
           }
       }
    
       public function hello() {
           $this->bar = 'world'; // __set is not called here
       }
    }
    
    $foo = new Foo;
    $foo->bar = 'world';         // __set is called here
    

    You can hack around this, by prepending your properties name with, say, an underscore. But this still feel very much like a hack:

    class Foo {
       private $_bar = 'hello';
       private $_baz = 0;
    
       public function __set($var, $value) {
           $actual_var = "_$var";
           switch ($var) {
               case 'bar':
                   $this->$actual_var .= $value;
                   break;
               case 'baz':
                   $this->$actual_var += $value;
                   break;
               default:
                   $this->$var = $value;
           }
       }
    
       public function hello() {
           $this->bar = 'world'; // __set is called here too
       }
    }
    

    In conclusion, I think you’re better off defining indiviual setters/getters method for each property to remove any ambiguity whatsoever:

    class Foo {
        private $bar = 'hello';
    
        public function setBar($value) {
            $this->bar .= $value;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Like I understand, @synthesize actually is generating the Getters and Setters. But what's @property
Can anybody tell me whether its posible to override doctrine2 persistentobject magic getters\setters? i'd
Since properties can have public and private getters / setters, is there still a
In C# you can create getter/setters in a simpler way than other languages: public
How can you describe the parameters and return type (getter/setter) of your PHP functions?
In my ActionScript3 class, can I have a property with a getter and setter?
Should you declare the getters/setters of the class inside the .h file and then
I recently read that getters/setters are evil and I have to say it makes
Should simple JavaBeans that have only simple getters and setters be unit tested?? What
I am rewriting a project so that it uses getters and setters to reference

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.