2

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?

avetisk
  • 11,651
  • 4
  • 24
  • 37
  • 1
    I cannot really answer your question concerning the php part, however, I do want to comment that using an interface is imho a very robust and solid solution. Nothing ugly about it. At least, not in the example you provide. – Henri Mar 27 '10 at 08:51
  • @Henri: As I said in my post "Moreover, you *can't* use that trick with *static classes*." – avetisk Mar 28 '10 at 12:31

2 Answers2

6

PHP really doesn't provide you an elegant way of handling this. Without meaning to start a language flamewar, I'm going to gingerly suggest that your design skills and needs have probably exceeded the limitations of your tool. PHP is a lightweight scripting language that's had a lot of pseudo-OOP features bolted onto it, but at its core, it wasn't ever designed for elegant enterprise architecture.

Dan Story
  • 9,985
  • 1
  • 23
  • 27
  • 2
    Please explain what is a language fit for elegant enterprise architecture then. Also, please explain why PHP is ranked #3 at the Tiobe index and why the largest social network in the world runs PHP? Also, please explain why argument.caller was deprecated in JavaScript1.3 when it was elegant to be able to implicitly access the outside scope from the callee. – Gordon Mar 27 '10 at 10:50
  • 3
    Popularity is extremely loosely correlated to design quality, if at all. As I said above, my intent was to avoid starting a language flame war, and I explicitly avoided making any other platform suggestions for exactly that reason. If you'd like to debate language merits we can do it offline, but this isn't the place for that argument. – Dan Story Mar 27 '10 at 11:18
  • 1
    Gingerly or not, but if your intention *wasn't* to start a flamewar, then I wonder why you insinuate PHP is somehow unfit for elegant *(however you define this - I consider breaking encapsulation poor design)* enterprise architectures at all. Everything starting from the your first *elegant* is just PHP bashing. – Gordon Mar 27 '10 at 12:51
  • very true. I am finding myself generating psuedo code to make things work. and by psuedo I mean ad hoc code that does stuff behind what the actual method was supposed to do by generating code at runtime. – andho Mar 11 '11 at 17:15
  • I asked this question two years ago, and now I'm slowing moving from PHP to nodejs. After using PHP for more than 7 years, it's sad to say but my conclusion is that PHP sucks, a lot. Though, it's only my own opinion. – avetisk Feb 04 '12 at 10:39
2

You can call debug_backtrace(FALSE), which will then not populate the object index. This will speed it up a little bit, but generally, debug_backtrace is to be avoided in production code, unless your app is software tool where speed is not an issue or when using it for error handling.

From what I understand, you want to

  • have an implicit reference to the caller available in the callee and
  • outside access to private and protected properties to selected classes.

Both does not exist in PHP (and breaks encapsulation imho). For a discussion, please see

Gordon
  • 312,688
  • 75
  • 539
  • 559
  • 1
    Thanks for both links. I found myself that PHP dev team *"deciced against friend declarations"*: http://bugs.php.net/bug.php?id=34044 That's really too bad. Anyway, thanks for the advice! – avetisk Mar 27 '10 at 12:28