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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:18:05+00:00 2026-05-30T09:18:05+00:00

I think its quite a simple question but not sure. I have a class:

  • 0

I think its quite a simple question but not sure.

I have a class:

<?PHP
class PropertyTest {
    private $data = array();

    public function __set($name, $value) {
        $this->data[$name] = $value;
    }

    public function __get($name) {
        if (array_key_exists($name, $this->data)) {
            return $this->data[$name];
        }

        $trace = debug_backtrace();
        trigger_error(
            'Undefined property via __get(): ' . $name .
                ' in ' . $trace[0]['file'] .
                ' on line ' . $trace[0]['line'],
            E_USER_NOTICE);
        return null;
    }

    public function __isset($name) {
        echo "Is '$name' set?\n";
        return isset($this->data[$name]);
    }

    public function __unset($name) {
        echo "Unsetting '$name'\n";
        unset($this->data[$name]);
    }

    public function getHidden() {
        return $this->hidden;
    }
}
?>

Not sure why but the ‘code’ block is annoying as hell, anyway.

Just the basic magic get set really. But when I change the __get to pass by reference I cant do this anymore:

$object->$variableName = $variableValue;

I’m not sure why although I assume because it checks if it exists but since it has to return something by reference it will fail to do so if it doesn’t exists to begin with. The set function wont be called probably and even if I return a fake value it would never call the set function cause it ‘already exists/has a value’.

Am I understanding this correctly? If so, Is there a work around? If not how does it work and is there a workaround?

  • 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-30T09:18:06+00:00Added an answer on May 30, 2026 at 9:18 am

    Unless I’m missing something it’s working fine for me

    <?php
    class PropertyTest
    {
    
        private $data = array();
    
        public function __set($name, $value)
        {
            $this->data[$name] = $value;
        }
    
        public function &__get($name)
        {
            if(array_key_exists($name, $this->data))
                return $this->data[$name];
            return null;
        }
    
        public function __isset($name) 
        {
            return isset($this->data[$name]);
        }
    
        public function __unset($name) 
        {
            unset($this->data[$name]);
        }
    
        public function getHidden()
        {
            return $this->hidden;
        }
    }
    
    $oPropTest = new PropertyTest();
    $sField = 'my-field';
    
    $oPropTest->$sField = 5;
    
    var_dump($oPropTest);
    

    Outputs:

    bash-3.2$ php test-get-set.php
    object(PropertyTest)#1 (1) {
      ["data":"PropertyTest":private]=>
      array(1) {
        ["my-field"]=>
        int(5)
      }
    }
    

    One tweak I’d suggest for your __get implementation is to leverage the __isset method rather than re-implement the check (doing it 2 different ways as you are)

    public function __get($name)
    {
        if($this->__isset($name))
            return $this->data[$name];
        return null;
    }
    

    Regarding the idea of return-by-reference from __get; it will work, but be useless on anything but public attributes, since private and protected attributes won’t be settable directly through a reference outside the class scope.

    $oPropTest = new PropertyTest();
    $sField = 'my-field';
    
    $oPropTest->$sField = 5;      // sets $oPropTest->my-field to 5 (effectively)
    $iRef = $oPropTest->$sField;  // $iRef is a reference to $oPropTest->my-field
    $iRef = 6;                    // this will not set $oPropTest->my-field since it's private
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My question is a very simple one i'm sure. It's just I'm not quite
I guess this is a quite simple (read: very simple) question, but I have
Sorry if this question seems a bit complex but I think its all related
I want to make something that makes much sense, but I think its not
I have to ask this question because I'm stuck, and I think its because
my problem is quite simple, but very important in general - I think. I
I have really simple and lame question (i hope it's simple). I'm quite new
I think its easy to create form via only html and do not use
I often take the classes that linq2sql generates and create a simple data-only class
Hi this question or problem i have its very hard i have search and

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.