Get calling class

Get calling class

am 21.04.2008 11:50:02 von brunormbarros

I have the following:

class A {
function something($args) {
print $CLASS_THAT_IS_CALLING_THIS_FUNCTION;
}
}

class B {
function execute($args) {
$a = new A();
$a->something($args);
}
}

When executing B::execute($args), it should print B. I can't provide
the name of the class on the arguments. I want it, because A is only
supposed to access files from a folder with the name of the class, so
on B, A would access files/B/... On C, A would access files/C/, and so
on. Is there any way of doing this?

Re: Get calling class

am 21.04.2008 12:01:53 von Andreas Gustafsson

Bruno Rafael Moreira de Barros wrote:
> I have the following:
>
> class A {
> function something($args) {
> print $CLASS_THAT_IS_CALLING_THIS_FUNCTION;
> }
> }
>
> class B {
> function execute($args) {
> $a = new A();
> $a->something($args);
> }
> }
>
> When executing B::execute($args), it should print B. I can't provide
> the name of the class on the arguments. I want it, because A is only
> supposed to access files from a folder with the name of the class, so
> on B, A would access files/B/... On C, A would access files/C/, and so
> on. Is there any way of doing this?

i think you could use this:
http://uk3.php.net/debug_backtrace

- andreas

Re: Get calling class

am 21.04.2008 20:16:41 von brunormbarros

On Apr 21, 11:01 am, Andreas Gustafsson
wrote:
> Bruno Rafael Moreira de Barros wrote:
>
>
>
> > I have the following:
>
> > class A {
> > function something($args) {
> > print $CLASS_THAT_IS_CALLING_THIS_FUNCTION;
> > }
> > }
>
> > class B {
> > function execute($args) {
> > $a = new A();
> > $a->something($args);
> > }
> > }
>
> > When executing B::execute($args), it should print B. I can't provide
> > the name of the class on the arguments. I want it, because A is only
> > supposed to access files from a folder with the name of the class, so
> > on B, A would access files/B/... On C, A would access files/C/, and so
> > on. Is there any way of doing this?
>
> i think you could use this:http://uk3.php.net/debug_backtrace
>
> - andreas

I actually thought of using __CLASS__ and I thought it worked, for a
while. But then it didn't. Might have just been my impression.

Using debug_backtrace:

class A {
function something($args) {
$s = debug_backtrace();
print "views/{$s[1]['class']}/file.phtml";
}

}

class B {
function execute($args) {
$a = new A();
$a->something($args);
}

}

B::execute(2);
?>

This worked perfectly. Unless you have C::somefunction, calling
B::somefunction, which then calls A::somefunction, but that will never
be needed, thankfully. Although a limitation in my scripts is
something I would never like, it's the only way I have of making sure
classes don't open folders that don't belong to them. Just me being
paranoid, once more.