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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:45:09+00:00 2026-05-19T23:45:09+00:00

Please excuse me if this question has been asked before, but I tried searching

  • 0

Please excuse me if this question has been asked before, but I tried searching for it with no satisfactory results.

I’m learning PHP (coming from a C++ background) and have come across the following ambiguity. The following two bits of code work exactly the same:

class A
{
    public $myInteger;
    public function __get($name) 
    { 
        return $this->$name; 
    }
    public function __set($name, $value)
    {
        $this->$name = $value;
    }
}

and

class A
{
    public $myInteger;
    public function __get($name) 
    { 
        return $this->name; 
    }
    public function __set($name, $value)
    {
        $this->name = $value;
    }
}

that is, in the class methods $this->$name and $this->name have the exact same function. I’m finding this a bit confusing, especially when considering that if you add the following code,

$myA = new A();
$myA->myInteger = 5;
$hereInt = $myA->myInteger;

echo "<p>" . $hereInt . "</p>";

it only works if there is no $ before myInteger. Could someone please explain the rationale behind this?

  • 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-19T23:45:10+00:00Added an answer on May 19, 2026 at 11:45 pm

    $this->$name and $this->name do not mean the same thing. The first is using a locally scoped variable $name to access the field of $this whose name is whatever $name contains, while the second accesses the name field directly.

    For example, the following will output something:

    $foo = new stdClass;
    $foo->bar = 'something';
    
    $baz = 'bar';
    echo $foo->$baz;
    

    In the case of __get and __set, $name contains the name of the property that was accessed at the call site; in your case, myInteger.

    In your example, the __get and __set methods are actually superfluous, since $myA->myInteger is public and can be accessed directly. __get and __set are only needed to catch access attempts to a property that is not declared explicitly in the class.

    For example, you might have a backing array that allows arbitrary “properties” to be set dynamically:

    class Foo
    {
        private $_values = array();
    
        public function __get($key)
        {
            if (isset($this->_values[$key]))
            {
                return $this->_values[$key]
            }
        }
    
        public function __set($key, $value)
        {
            $this->_values[$key] = $value;
        }
    }
    

    One thing that’s somewhat confusing about this aspect of PHP’s syntax is that a $ precedes a field declaration in a class, but there is none when accessing that field. This is compounded by the syntax for accessing static fields, which does require a $!

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

Sidebar

Related Questions

I could imagine this question has already been asked, but I actually could not
Please excuse this numpty question, but when I do ChannelFuture future = channel.write(message); future.addListener(new
I am currently learning ASP.NET MVC so please excuse my question if it has
Please excuse me if this is a very novice question, but I am little
I am a patterns newbie so please excuse this question if it sounds too
Please excuse the newbie question but I was wondering if I used Linq in
Please excuse the vague question title, but usually I don't do such kind of
This is my first question, so please excuse me for any mistake I make.
I'm new to MVC and php framework, so please excuse me for this simple
I'm new to EF so please excuse me if this is a noob question.

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.