Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find out the directory where the class instance was created?

Tags:

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?

like image 899
morepusto Avatar asked Oct 26 '19 21:10

morepusto


People also ask

How do I find the instance of a class in Java?

The instanceof operator in Java is used to check whether an object is an instance of a particular class or not. objectName instanceOf className; Here, if objectName is an instance of className , the operator returns true . Otherwise, it returns false .

What is working directory in Java?

The current working directory means the root folder of your current Java project. We can get the current working directory in Java using the following system property function: String cwd = System.

What does :: class do in PHP?

SomeClass::class will return the fully qualified name of SomeClass including the namespace. This feature was implemented in PHP 5.5. It's very useful for 2 reasons. You can use the use keyword to resolve your class and you don't need to write the full class name.

What is the current instance of a class?

The current instance of an object is the instance in which the code is currently executing.


1 Answers

Yes, you can use debug_backtrace()

class A {
    function __construct() {
        var_dump(dirname(debug_backtrace()[0]['file']));
    }
}
like image 122
Dharman Avatar answered Oct 18 '22 15:10

Dharman