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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:20:40+00:00 2026-06-09T20:20:40+00:00

<?php class BaseClass { protected $data = array(foo => 0, bar => 0, baz

  • 0
<?php
class BaseClass {
  protected $data = array("foo" => 0, "bar" => 0, "baz" => 0);

  public function __set($name, $value) {
    if( array_key_exists($name, $this->data)){
      $func = "set_$name";
      return call_user_func_array(array($this,$func),array($value));
    }
  }


  public function __get($name) {
    if ( array_key_exists($name, $this->data)){
      $func = "get_$name";
      return call_user_func_array(array($this,$func),array());
    }
  }

  public function __call($method, $args) {
    if (method_exists($this,$method)) {
      return call_user_func_array(array($this,$method),$args);
    }

    if (substr($method, 0, 4) == 'set_'){
      $var_name = substr($method, 4);
      if (!(array_key_exists($var_name, $this->data))){ 
        return FALSE;
      }
      $this->data[$var_name] = $args[0];
      return TRUE;
    }

    if (substr($method, 0, 4) == 'get_'){
      $var_name = substr($method, 4);
      if (!(array_key_exists($var_name, $this->data))){ 
        return FALSE;
      }
      return $this->data[$var_name];
    }
  }
}

class SubClass extends BaseClass {
  protected $baz_changed = FALSE;

  public function set_baz($value) {
    if ($value != $this->baz){
      print "\n\nthis->data BEFORE SET:  ";
      print_r($this->data);
      print "\n\nthis->baz:  ";
      print_r($this->baz);
      print "\n\nPASSED baz:  ";
      print_r($value);
      $this->baz = $value;
      print "\n\nbaz AFTER SET:  ";
      print_r($this->baz); // appears it was set 
      print "\n\nDATA ARRAY:  ";
      print_r($this->data);  // but it wasn't ... what gives? 
      $this->baz_changed = TRUE;
    }
  }
}

$sc = new SubClass();
$sc->foo = 1;
print "\n\$sc->foo = $sc->foo\n";
$sc->baz = 5;
print "\$sc->baz = $sc->baz\n";
?>

I get the following results are not as I would have expected:

 $sc->foo = 1


 this->data BEFORE SET:  Array (
     [foo] => 1
     [bar] => 0
     [baz] => 0 )


 this->baz:  0

 PASSED baz:  5

 baz AFTER SET:  5

 DATA ARRAY:  Array (
     [foo] => 1
     [bar] => 0
     [baz] => 0 ) $sc->baz = 5

As you can see, it appears that baz is set but it never gets set in the data array. Can anyone explain why and how to fix this?

EDIT: Fixed formatting of the results and adding more context to this code section because stackoverflow is complaining I do not have enough.

EDIT: Just noticed that at the end it says $sc->baz = 5. But the data array isn’t updated. This is not expected I would rather that the data array version of baz were updated. Instead of a new instance variable created in SubClass.

  • 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-09T20:20:42+00:00Added an answer on June 9, 2026 at 8:20 pm

    You’re attempting to recursively call __set, and PHP specifically disallows this.

    • In __set, you call set_baz
    • In set_baz, you do $this->baz = 5
    • This would invoke __set, except PHP prevents this from happening. If it didn’t, your program would never end.

    You cannot trigger __set if you’re already within __set. Instead, you are dynamically defining a new member variable called $this->baz exactly as if __set did not exist. If you var_dump your object, you will find that it now contains both $data and $baz members.

    Inside set_baz, you need to explicitly write to $this->data. You cannot write to $this->baz.

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

Sidebar

Related Questions

the class, which is content.php class content { private $vars = array(); public function
class_1.php class class_1 { public function __construct() { require_once $_SERVER['DOCUMENT_ROOT'].'/array.php'; } } class_2.php class
<?php class a { public function foo() { echo __METHOD__ . PHP_EOL; } }
This is dbconnect.class.php: <?php class Connect { public $error; protected $db; public function __construct()
I am having PHP class class SurveyQuestion extends CActiveRecord { public function relations() {
Here is the class: functions.php class buildPage { public function Set($var,$val){ $this->set->$var = $val;
I have a php class, RegistrationController, <?php class RegistrationController extends ParentController { function __construct()
I have a PHP class that stores a complex multidimensional array, and rather than
Here are the domain model classes: public abstract class BaseClass { ... } public
Why PHP class does not return <h1>404</h1><p>Not found</p> ? class checkid{ public $title; public

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.