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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:31:04+00:00 2026-06-10T21:31:04+00:00

i dont seem to understand why the code below only prints TEST two times.

  • 0

i dont seem to understand why the code below only prints “TEST” two times.

<?php

class A {
    private $test = "TEST<br />";

    public static function getInstance() {
        return new self();
    }

    public static function someStaticMethod() {
        $a = new self();
        $a->test;
    }

    public function __get($args) {
        echo $this->$args;
    }
}

/* echo's "TEST" */
$a = new A();
$a->test;

/* echo's "TEST" */
$a2 = A::getInstance();
$a2->test;

/*
No output... eeerhm... how come?
Why is $a->test (inside someStaticMethod()) not being overloaded by __get ??
*/
A::someStaticMethod();

?>

PHP site says (link):

Property overloading only works in object context. These magic methods will not be triggered in static context. Therefore these methods should not be declared static. As of PHP 5.3.0, a warning is issued if one of the magic overloading methods is declared static.

But i think they are trying to say u should declare the magic methods static. e.g.:

public static function __get(){}

Plus the fact the i am in fact using it in object context. $a = new self(); returns an instance from class A in variable $a. Then i am using $a->test (object context imo?) to fetch the private “test” variable which in turn should get overloaded…

i am confused…

  • 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-10T21:31:05+00:00Added an answer on June 10, 2026 at 9:31 pm

    It seems that, in the context of A:: someStaticMethod, PHP allows you to access the private variable $test directly, so the magic method is not executing. If you echo $a->test; from there, you’ll see it’s being accessed.

    That is the expected behaviour, according to the PHP Manual:

    Objects of the same type will have access to each others private and protected members even though they are not the same instances. This is because the implementation specific details are already known when inside those objects.

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

Sidebar

Related Questions

I understand how late static binding works, but I can't seem to come up
My _spec file includes the code below, but my test fails with: NameError in
I don't seem to understand the purpose of XMLString::transcode(XMLCh*) and XMLString::transcode(char*) , because obviously
I dont seem to be able to find any evidence of multiple groupby being
I've been reading about this on StackOverflow and facebook doc, yet I dont seem
Regarding the terminate handler, As i understand it, when something bad happens in code,
First of I am new to CSS and don't seem to understand how classes
I don't understand what this code is doing, I'm wanting to run a command
Referencing the code below, I have a list of Error objects that has been
Can someone tell me what is going on with the code below: 'Variables at

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.