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

The Archive Base Latest Questions

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

In the following example class Employee { var $name; var $city; protected $wage; function

  • 0

In the following example

class Employee
{
    var $name;
    var $city;
    protected $wage;
    function __get($propName)
    {
        echo "__get called!<br />";
        $vars = array("name","city");
        if (in_array($propName, $vars))
        {
            return $this->$propName;
        } else {
            return "No such variable!";
        }
    }
}
$employee = new Employee();
$employee->name = "Mario";
echo $employee->name."<br />";
echo $employee->age;

The output is:

Mario
__get called!
No such variable!

… confuses me, I understand that “__get called!” appears when $employee->name is accesed, but why does it not appear even when $employee->age is accesed? I mean… it’s there but it seems like the __get considers only the return in the if-else statement. How is that possible?

Observation : Of course __get runs when it detects $age does not exist when read, but …when it does run, why doesn’t the echo in it work?

Conclusion: the “__get called!” appeared because of one call of __get, that in which $employee->age is read.

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

    Your assumptions are wrong.

    I understand that “__get called!” appears when $employee->name is accesed

    No, it doesn’t. __get is only invoked when you attempt to access a member which does not exist. You have defined a publicly accessible member named $name, this is what the line var $name does. Reading/writing to $employee->name will never invoke the magic __get or __set_ methods.

    It is not $employee->name that is causing "__get called" to be printed; $employee->name is returning "Mario", the value you assigned to that member, and that is what is being printed. It is $employee->age that is causing "__get called" to be printed.

    I’ve tried to better explain the lines and what they do in respect to __get:

    $employee = new Employee();
    $employee->name = "Mario";      # assign to $name
    echo $employee->name."<br />";  # read "Mario" from $name, does NOT invoke  __get
    echo $employee->age;            # attempt to read $age, invokes __get
    

    RE: Your observation

    Observation : Of course __get runs when it detects $age does not exist when read, but …when it does run, why doesn’t the echo in it work?

    The echo does work. The reason you are only seeing one "__get called!" is because __get is only called once, by attempting to access $employee->age.

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

Sidebar

Related Questions

As a simplified example, I have the following data classes: public class Employee {
Take for example following code from a class: public class Employee : IEntity {
I have the following example class: Test.h: @interface Test : UIButton { NSString *value;
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
I have the following example code: class A(object): def __init__(self, id): self.myid = id
Let's have a following example: public class X { } public class Y {
Take the following class as an example: class Sometype { int someValue; public Sometype(int
Take the following example, I have a class public class SomeItem { public string
Given the following example (using JUnit with Hamcrest matchers): Map<String, Class<? extends Serializable>> expected
For class specific new_handler implementation, i came across the following example in book effective

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.