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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:44:20+00:00 2026-06-01T11:44:20+00:00

Consider this class: class test { public function __set($n, $v) { echo __set() called\n;

  • 0

Consider this class:

class test
{
    public function __set($n, $v)
    {
        echo "__set() called\n";
        $this->other_set($n, $v, true);
    }

    public function other_set($name, $value)
    {
        echo "other_set() called\n";    
        $this->$name = $value;
    }

    public function t()
    {
        $this->t = true;
    }
}

I am overloading PHP’s magic __set() method. Whenever I set a property in an object of the test class, it will call __set(), which in turn calls other_set().

$obj = new test;
$test->prop = 10;

/* prints the following */
__set() called
other_set() called

But other_set() has the following line $this->$name = $value. Shouldn’t this result in a call to __set(), causing infinite recursion?

I theorized that it would call __set() only when setting things outside the class. But if you call the method t() you can see it clearly goes through __set() too.

  • 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-01T11:44:21+00:00Added an answer on June 1, 2026 at 11:44 am

    __set is only called once per attempt for a given property name. If it (or anything it calls) attempts to set the same property, PHP won’t call __set again — it’ll just set the property on the object.

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

Sidebar

Related Questions

Consider this class: class Test { public $obj; function __construct ($obj) { $this->obj =
Consider this code : class App { public static function log($msg) { echo $msg;
Consider this example @Scope(ScopeType.SESSION) @Name(test) @BypassInterceptors public Class Test { @Unwrap public List<String> test()
Consider this source: public partial class FormTest : Form { private Test test {
Consider this code sample: public class Human { public string Value { get; set;}
Consider this code: class test { public static void main(String[] args) { test inst_test
Consider this code: class Test { public: Test() { i = 0; } private:
Consider class Foo { public $att; public function __construct ( $a ) { $this->att
Consider this overly simple test: class foo { public: foo(int i); template< typename T
Consider this example : import java.lang.reflect.Field; public class Test { public static void main(String[]

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.