1

Is there any way of identifying if a function was called from within the same class in PHP? Besides using something like debug_backtrace ?

Update:

This is what I'm currently doing:

class Alex {
  function __construct()
  {
     $this->internal = false;
  }

  function a()
  {
    $this->internal = true;
    $this->b();
  }

  function b()
  {
    if($this->internal) 
      // ...
    else
      // ...
  }
}
Alex
  • 7,538
  • 23
  • 84
  • 152
  • Are you looking for general or one case solution? Do you need to provide list of functions and if they were called more than one time or check it for exact function? – Michael Mar 07 '13 at 15:44
  • possible duplicate of [Caller function in PHP 5?](http://stackoverflow.com/questions/190421/caller-function-in-php-5) – aorcsik Mar 07 '13 at 15:46
  • On case solution. I need to check if the function (when called), is called within the class (by another function) or from outside (as a method belonging to the controller) – Alex Mar 07 '13 at 15:46
  • What exacly do you want to achieve here? Does your code making troubles? Or you dont want to pass this extra parameter? – Michael Mar 07 '13 at 15:54
  • @aorcsik all the solutions rely on `debug_backtrace`, which I am already aware of – Alex Mar 07 '13 at 15:56
  • @360flowMichael The purpose of that, is to know what to return within the function. Because if it's called from outside (like `http://site.com/controller/method` I will have to return something different as if is called from within the class. But well, seams for now that the solution I use, it's the best for now – Alex Mar 07 '13 at 15:58
  • I know that you already mentioned `debug_backtrace`, but why are you against it? If you want to "trace" where a certain function/method is called from then that is the only place you can look at. There's nothing wrong with `debug_backtrace`. What worries me more is that you actually need functionality like that, but that's a different story – w00 Mar 07 '13 at 16:08
  • I'm not against it, I was just trying to see if any other ways (besides the 2 mentioned). Well, I guess I'll have to decide which one to use, and go for it – Alex Mar 07 '13 at 16:10

3 Answers3

2

I 'm not sure why you would want to do that; this kind of requirement is suspicious, and usually for good reason.

That said, you can make a protected clone of the public function with an additional parameter that tells you if the caller was internal or external, and make the public version defer to the protected implementation.

class Before
{
    public foo() { /* code here */ }
}

class After
{
    public foo() { $this->fooCore(false); }
    protected fooCore($calledFromInside = true) { /* code here */ }

    // At this point you should make sure that you never call $this->foo()
    // directly from inside class After
}
Jon
  • 428,835
  • 81
  • 738
  • 806
  • interesting work around, but no... this would just make things way to complicated. Tnx anyway – Alex Mar 07 '13 at 15:51
  • Unfortunately, this won't work if you need to know if `__call` was invoked by the class itself, or externally. – Nick Feb 28 '17 at 20:33
0

Not that I'm aware of. I solved a similar problem by having an extra optional parameter that passed a value when called from inside the class.

HTH :)

splash21
  • 799
  • 4
  • 10
-1

It is possible to debug PHP for instance in NetBeans like you can see to debugging in NetBeans. Also, you can find useful tools onsseful PHP tools, like Webgrind or Xdebug.

Mihai8
  • 3,113
  • 1
  • 21
  • 31
  • the `debug_backtrace` I've mentioned it... and on the live server, I don't have NetBeans... – Alex Mar 07 '13 at 15:53