hypertensy
hypertensy

Reputation: 235

Find out the directory where the class instance was created?

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?

Upvotes: 5

Views: 79

Answers (1)

Dharman
Dharman

Reputation: 33402

Yes, you can use debug_backtrace()

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

Upvotes: 4

Related Questions