Page 1 of 1

can anyone think of a way to determine path of calling func

Posted: Fri Oct 02, 2009 8:43 pm
by kage
ok so ill outline what im trying to do. i have a file structure like this
-root
-->core/
---->class1.php
-->modules/
---->module1/
-------->php1.php
-------->templates/
---------->template1.htm
---->module2/
-------->php2.php
-------->templates/
now php1 and php2 both create objects of class1, however it is important for object class1 to know the path of 'templates'. this is being used in my templating system, by default the template dir is the dir of class1.php , so when i want to use a template i have to give the relative directory (for a template used by module1 for example i would have to say '../modules/module1/templates/***TEMPLATEFILE***' ) ... i can do that but its kind of messy, what i would like to do is when i create a class1 object for it to construct knowning the dir to my templates folder for that module. so in the constuctor for class1 it would be useful for it to know the directory which contains the calling function.

i have come up with a temporary solution which supplies an optional argument to the class1 constructor. so if i said something like

Code: Select all

$obj = new class1('module1');
it would know that im looking for the template dir '../modules/module1/templates'
this works better then having to supply a full relative path from class1 but is still cumbersome. what i would really like is to be able to say

Code: Select all

$obj = new class1();
and have the constructor just know where the call came from.

can anyone think of a way of doing that, i might just be stuck doing what im doing now but thought i would ask
thanks for your replies/thought

Re: can anyone think of a way to determine path of calling func

Posted: Sat Oct 03, 2009 3:46 am
by Mark Baker
Unless the call explicitly tells the function/method where it came from or pass the template path to the method (passing it in as an additional parameter)
then debug_backtrace() is the only way.

But there's very few circumstances where you should need to use debug_backtrace(). A parameter or setting a propertyy in the class instance should be the solution.

Code: Select all

$obj = new class1('module1');
or

Code: Select all

$obj = new class1($templateDirectory);
is the correct way to do this