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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:37:57+00:00 2026-06-18T19:37:57+00:00

I was just mocking up an intercept class I am working on. Idea is

  • 0

I was just mocking up an intercept class I am working on. Idea is you instantiate the class through the Intercept class and then use that object as if it was the class you are intercepting. A specified callback is then run every time the class gets called. Here is the code:

<?php
class Intercept {

    protected $class = null;
    protected $callback = null;

    public function __construct($class, $callback = null) {

        $this->class = new $class();
        $this->callback = $callback;
    }

    protected function run_callback() {

        $this->callback();
    }

    public function __get($name) {

        $this->run_callback();
        return $this->class->$name;
    }

    public function __set($name, $value) {

        $this->run_callback();      
        return $this->class->$name = $value;
    }

    public function __isset($name) {

        $this->run_callback();
        return isset($this->class->$name);
    }

    public function __unset($name) {

        $this->run_callback();
        unset($this->class->$name);
    }

    public function __call($method, $args) {

        $this->run_callback();
        return call_user_func_array(array($this->class, $method), $args);
    }

    public function __toString() {

        $this->run_callback();
        return $this->class;
    }

    public function __invoke() {

        $this->run_callback();
        return $this->class();
    }
}

class test {
    public function hello() { 
        return 'world'; 
    }
}

$closure = function() {
    echo 123;
};

$test=new Intercept('test', $closure);
echo $test->hello();

Now, running the code above should display ‘world123’. But for some strange reason which I cannot see, it ends up timing out. I tried it on my local machine, and on various php 5.4 tester sites online. Same thing happens. I have narrowed it down to the closure being run ($this->callback()) in the run_callback() method. If i just remove the $this->callback(), it works fine. Why is this happening?

Edit, while I was writing this question, I figured out that instead of:

$this->callback();

Doing this will stop the timeout:

$closure = $this->callback;
$closure();

It seems that the __call method gets called everytime I try to run the closure directly from the class properties. Is this expected behaviour or have I stumbled apon a PHP bug?

  • 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-18T19:37:58+00:00Added an answer on June 18, 2026 at 7:37 pm

    Pretty sure it’s because you have an infinite loop with the function call stack. When you do

    $this->callback();
    

    You’re trying to execute the member function callback(), which doesn’t exist, so __call() gets executed, which tries callback() again, which doesn’t exist, so __call() gets executed, and so on and so forth.

    You should be using something along the lines of:

    call_user_func( $this->callback);
    

    Or, like you’ve edited, this will work too:

    $closure = $this->callback;
    $closure();
    

    Hopefully this clears up what is happening. The timeout is just occurring because you’re running out of resources (in this case, time). The underlying problem is the infinite loop.

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

Sidebar

Related Questions

I'm working with a very simple web service that uses a base class to
I'm trying to run some NUnit tests that use NMock2 for mocking. These tests
just wanted to ask where I define initial class properties? From other languages I
I just recently read about Mocking objects for unit testing and currently I'm having
I am writing a small mocking class to do some tests. But this class
I'm asking because I'm trying to use a mocking framework (Mockito) which does not
Anybody know of a mocking framework that supports C# 4.0? Doesn't matter which one
Below I am just trying to mock a class named TestWrapper and set 'allowing'
I'm curious as to what method people like to use for mocking and why
I have just see the Moles mocking framework from Microsoft Research, before I spend

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.