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 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

Related Questions

I need this roadmap of a Hibernate managed object instance. First, I create an
i´m currently learning Core Data. Core Data is great but i can not explain
I can create new managed objects inside my app, in a separate view I
I followed this way. To create a workspace to manage your source-controlled files 1.
I am developing this JavaScript game, that manages it's loading of assets, prior anything
I am writing a program for class that manages a Hotel. This Report1 function
Regualar expression: <img[^>]+src\s*=\s*[']([^']+)['][^>]*> This works fine when 'src' is in lowercase and manages both
I have this var manager = context.ManagerInfoes.Select(m => m.Guid == managerGuid).First(); how can i
I am trying to create a pageing and sorting object data source that before
I’m using NHibernate for data access, but accessing it through a façade layer. This

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.