PHP: friend classes and ungreedy caller function/class

Posted by avetis.kazarian on Stack Overflow See other posts from Stack Overflow or by avetis.kazarian
Published on 2010-03-27T08:47:31Z Indexed on 2010/03/27 8:53 UTC
Read the original article Hit count: 280

Filed under:
|
|
|
|

Is there any way to get the caller function with something else than debug_backtrace()?

I'm looking for a less greedy way to simulate scopes like friend or internal.

Let's say I have a class A and a class B.

Until now, I've been using debug_backtrace(), which is too greedy (IMHO).

I thought of something like this:

<?php

    class A
    {
        public function __construct(B $callerObj) {}
    }

    class B
    {
        public function someMethod()
        {
            $obj = new A($this);
        }
    }
?>

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?

One way could be using an interface to aggregate:

public function __construct(CallerInterface $callerObj)

But it's still an ugly code.

Moreover, you can't use that trick with static classes.

Have any better idea?

© Stack Overflow or respective owner

Related posts about php

Related posts about scope