allenskd
allenskd

Reputation: 1805

How to know which file is initializing the object?

Is it possible to know in which file the object is being initialized?

For example, I want to prevent the class from running if it's in X file. I know it defeats the purpose of being re-usable, etc etc. I don't know if it's possible in PHP or possible at all.

Upvotes: 2

Views: 92

Answers (1)

Mike
Mike

Reputation: 5046

In your constructor, examine the result of debug_backtrace(). The first frame describes the function (a file and line number) of the constructor itself, whereas the next frame describes the code that called the constructor.

This works anywhere, not just within functions or methods.

Upvotes: 4

Related Questions