Find out the directory where the class instance was created?

Viewed 56

Is it possible to know the directory where the class instance was created? The path of the directory I need to know in the class to save the result in the same directory. So far I've only come up with a "bike" transmitting:

(new BasicTest('test_value', dirname(__FILE__))

in the class constructor itself. Are there other ways?

1 Answers

Yes, you can use debug_backtrace()

class A {
    function __construct() {
        var_dump(dirname(debug_backtrace()[0]['file']));
    }
}
Related