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
      // ...
  }
}
4

3 回答 3

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
}
于 2013-03-07T15:46:22.433 回答
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 :)

于 2013-03-07T15:47:10.360 回答
-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.

于 2013-03-07T15:45:31.010 回答