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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:41:09+00:00 2026-06-18T16:41:09+00:00

I am attempting to write a listener on a variable in a class I

  • 0

I am attempting to write a “listener” on a variable in a class I cannot modify. I am extending the class in question, unsetting the properties I want to listen to, and then using __set to intercept writes to that variable. At that point I compare with the previous version and report if there are changes.

class A {
    var $variable;

    ...
}

class B extends A {
    var $new_variable

    function __construct() {
        parent::__construct();
        unset($this->variable);
    }

    function __set($thing, $data) {
        if ($thing == 'variable') {
            // Report change
            // Set $new_variable so we can use __get on it
        }
    }

    public function __get($var) {
        if (isset($this->$var)) {
            // Get as normal.
            return $this->$var;
        } elseif ($var == 'variable' && isset($this->new_variable)) {
            return $this->new_variable;
        }
    }

    ...
}

This works if I modify the class in question directly instead of via an extended class, removing the declaration of the variable and introducing the setter and getter methods. The problem is when I use the pattern shown above, the unset() call does not seem to actually remove the variables inherited from the parent class, thus rendering the __set method unable to intercept the variable’s value.

So far this seems to me to be the only way I can watch the variables changes, but I do not want to hack the core of the framework, only inspect it’s handy work (a parser). Is there a possibility of making this work, or another way to approach this problem?

  • 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-18T16:41:11+00:00Added an answer on June 18, 2026 at 4:41 pm

    Mmm, this is weird. The following code works fine:

    class A 
    {
        var $variable;
    }
    
    class B extends A 
    {
        var $new_variable;
    
        function __construct() 
        {
            unset($this->variable);
        }
    
        function __set($thing, $data) 
        {
            if ($thing == 'variable') 
            {
            echo "\nThe variable value is '" . $data . "'\n";
            }
        }
    }
    
    $b = new B();
    $b->variable = 'Intercepted'; //Output: The variable value is 'Intercepted'
    $b->new_variable = 'Not intercepted'; // No output
    

    Can you tell me if this piece of code does what you need and, if it doesn’t, what would you need instead?

    HTH

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

Sidebar

Related Questions

I'm attempting to write a very basic script using javascript. What I want to
I'm attempting to write a simple buffer overflow using C on Mac OS X
I'm attempting to write a CompiledQuery using Linq-to-Entities that will replace a stored procedure
In attempting to write a python script to access GCS using service-based authorization, I
I'm attempting to write a custom widget and I want to use existing UI
I'm attempting to write a C# managed class to wrap SHGetKnownFolderPath, so far it
I am attempting to write a .dll using a library that comes with some
I am attempting to write a spider using CasperJS used to parse pages and
I am attempting to write a unit test for a class's __init__ : def
I am attempting to write my own template queue class to learn how to

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.