title: PHP: friend classes and ungreedy caller function/class
body: <p>Is there any way to get the caller function with something else than debug_backtrace()?</p>
  
  <p>I'm looking for a less greedy way to simulate scopes like <em>friend</em> or <em>internal</em>.</p>
  
  <p>Let's say I have a class A and a class B.</p>
  
  <p>Until now, I've been using <code>debug_backtrace()</code>, which is too greedy (IMHO).</p>
  
  <p>I thought of something like this:</p>
  
  <pre><code>&lt;?php
  
      class A
      {
          public function __construct(B $callerObj) {}
      }
  
      class B
      {
          public function someMethod()
          {
              $obj = new A($this);
          }
      }
  ?&gt;
  </code></pre>
  
  <p>It might be OK if you want to limit it to one specific class, but let's say I have 300 classes, and I want to limit it to 25 of them?</p>
  
  <p>One way could be using an interface to aggregate:</p>
  
  <p><code>public function __construct(CallerInterface $callerObj)</code></p>
  
  <p>But it's still an <em>ugly code</em>.</p>
  
  <p>Moreover, you <strong>can't</strong> use that trick with <strong>static</strong> classes.</p>
  
  <p>Have any better idea?</p>