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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:58:20+00:00 2026-06-16T02:58:20+00:00

I recently went to an interview and my code I supplied had magic functions

  • 0

I recently went to an interview and my code I supplied had magic functions to get and set variables. My code was as follows:

public function __get($name){
    try { 
        return $this->$name;
    } catch (Exception $e) { 
        throw new Exception('Trying to get a variable "'.$name.'" that does not exist.'); 
    }
}

In the interview the guy asked me about the visibility on my variables, I had private ones set but these were now accessible by using magic functions. Essentially I failed the interview on this point, so I wanted to understand more. I was following a tutorial from PHP Master and found a different __get, I have tried to break it but it seems to work, but in a strange way.

I call __get('test') to get my variable _test but if it is set to private it calls itself again and tells me that it cannot access __test. I do not really understand why it calls itself again.

public function __get($name)
{
    $field = '_' . strtolower($name);

    if (!property_exists($this, $field)){
        throw new \InvalidArgumentException(
            "Getting the field '$field' is not valid for this entity"
        );
    }

    $accessor = 'get' . ucfirst(strtolower($name));
    return (method_exists($this, $accessor) && is_callable(array($this, $accessor))) ?
        $this->$accessor() : $this->$field;

}

Can anyone give me some pointers on proper use of __get and __set when using visibility in a class and why this function would call itself again.

I have read the other posts here but I am still struggling with this concept.

  • 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-16T02:58:21+00:00Added an answer on June 16, 2026 at 2:58 am

    I just bumped into this question and there is a little thing that may be worth clarifying:

    I do not really understand why it calls itself again.

    The code is not calling itself again but trying to execute a custom getter if there is one defined. Let me break down the method execution:

    public function __get($name)
    {
    

    As already explained in the other answers and here, the __get() magic method is called when you are trying to access a property that is not declared or not visible in the calling scope.

    $field = '_' . strtolower($name);
    
    if (!property_exists($this, $field)){
        throw new \InvalidArgumentException(
            "Getting the field '$field' is not valid for this entity"
        );
    }
    

    Here it just checks that a property with an underscore pre-appended exists in the class definition. If it doesn’t, an exception is thrown.

    $accessor = 'get' . ucfirst(strtolower($name));
    

    Here it creates the name of the getter to call if it exists. Thus, if you try to access a property named email and there is a private member called _email the $accessor variable will now hold the 'getEmail' string.

    return (method_exists($this, $accessor) && is_callable(array($this, $accessor))) ?
        $this->$accessor() : $this->$field;
    

    The final part is a bit cryiptic, since many things are happening in one line:

    • method_exists($this, $accessor). Checks if the receiver ($this) has a method with $accessor name (in our example, getEmail).
    • is_callable(array($this, $accessor)). Checks that the getter can be called.
    • If both conditions are met, the custom getter is called and its return value is returned ($this->$accessor()). If not, the property contents are returned ($this->$field).

    As an example consider this class definition:

    class AccessorsExample
    {
    private $_test1 = "One";
    private $_test2 = "Two";
    
    public function getTest2()
    {
    echo "Calling the getter\n";
    return $this->_test2;
    }
    
    public function __get($name)
    {
        $field = '_' . strtolower($name);
    
        if (!property_exists($this, $field)){
            throw new \InvalidArgumentException(
                "Getting the field '$field' is not valid for this entity"
            );
        }
    
        $accessor = 'get' . ucfirst(strtolower($name));
        return (method_exists($this, $accessor) && is_callable(array($this, $accessor))) ?
            $this->$accessor() : $this->$field;
    
    }
    }
    

    and then run:

    $example = new AccessorsExample();
    echo $example->test1 . "\n";
    echo $example->test2 . "\n";
    

    You should see:

    One
    Calling the getter
    Two
    

    HTH

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

Sidebar

Related Questions

We recently went into private beta on our flagship product and had a small
This question was recently asked to me in an interview for which i went
I recently went for a Java Interview where the interviewer asked me a question
I had an interview recently where I was asked to produce the traditional FizzBuzz
Recently I went thru the code for accessing the camera using flash ActionScript3 and
We recently had a web app that went out to site acceptance testing where
I recently read about using GCC's code generation features (specifically, the -finstrument-functions compiler flag)
We recently had a situation where a production machine went down with a massive
Recently one of our app servers went down, when it was rebooted the Python
I have recently picked up a project from a few months ago. Went to

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.