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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:55:56+00:00 2026-05-13T07:55:56+00:00

This manages to create a new property on the object. But, can someone explain,

  • 0

This manages to create a new property on the object. But, can someone explain, with supporting links, why setAttrib behaves in two different ways? Why doesn’t it cause a… wait for it… stack overflow!!??

class Test
{
  public function setAttrib( $key, $value ) {
    echo "setAttrib\n";

    // first time: calls $this->__set($key, $value)
    // second time: just sets a public property (but, when exactly was it created?)
    $this->$key = $value;
  }

  public function __set( $key, $value ) {
    echo "__set\n";
    $this->setAttrib($key, $value);
  }
}

$test = new Test();
$test->setAttrib('hey', 'It works');
var_dump($test);

produces…

setAttrib
__set
setAttrib
object(Test)#1 (1) {
  ["hey"]=>
  string(8) "It works"
}

Edit: I’m not looking for an alternative. I’m looking for the reason why this works.

  • 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-13T07:55:57+00:00Added an answer on May 13, 2026 at 7:55 am

    You are not the only one who seems to have notice that non-recursive behaviour : this comment on the manual’s page states :

    2 – PHP will not recursively call one
    magic method from within itself (at
    least for the same $name).

    And, a bit later on the same page, there is this one, which states :

    The recursion detection feature can
    prove especially perilous when using
    __set. When PHP comes across a
    statement that would usually call
    __set but would lead to recursion,
    rather than firing off a warning or
    simply not executing the statement it
    will act as though there is no __set
    method defined at all.
    The default
    behaviour in this instance is to
    dynamically add the specified property
    to the object thus breaking the
    desired functionality of all further
    calls to __set or __get for that
    property.

    And, on PHP’s bugtracker, there is #47215 : magic method __set() is bypassed on recursive call, which says :

    Magic method __set() is bypassed on
    recursive call.
    PHP automatically
    creates a property on instance instead
    of recursively calling __set() or
    instead of throwing a recursivity
    error

    And it has been closed as :

    Thank you for taking the time to write
    to us, but this is not a bug.

    That bug-report, itself, points to this blog-post, which ends by this sentence (quoting, emphasis mine) :

    After all I think it may not be a bug
    but expected behaviour, otherwise we
    could not be able to define object
    properties from within __set()
    method
    .

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

Sidebar

Ask A Question

Stats

  • Questions 256k
  • Answers 256k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Since ref and out parameters allow a method to work… May 13, 2026 at 10:29 am
  • Editorial Team
    Editorial Team added an answer Looks like IE doesn't treat your XML nodes as DOM… May 13, 2026 at 10:29 am
  • Editorial Team
    Editorial Team added an answer I'm pretty sure the latter is the case. You're shielded… May 13, 2026 at 10:29 am

Related Questions

One-line summary: What is the best practice for unhooking event handlers created in the
I need help with the following problem: I have a class with two properties.
I'm trying to determine how best to architect a .NET Entity Framework project to
I have a CRUD-heavy ASP.NET application with all the business logic in Stored Procedures.
I've formerly used L2S and am looking at using NHib along with the Sharp

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.