Download the PHP package brightnucleus/namespace-backtracer without Composer
On this page you can find all versions of the php package brightnucleus/namespace-backtracer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download brightnucleus/namespace-backtracer
More information about brightnucleus/namespace-backtracer
Files in brightnucleus/namespace-backtracer
Package namespace-backtracer
Short Description Get the namespace of the calling object.
License MIT
Informations about the package namespace-backtracer
Bright Nucleus Namespace Backtracer
Get the namespace of the calling object, by scanning the debug backtrace, skipping a known set of namespaces & functions in the process.
Table Of Contents
- Overview
- Installation
- Basic Usage
- Setting Things Up
- Making The Call
- Contributing
- License
Overview
This class/trait allows you to traverse the debug_backtrace()
to find out what the calling class/function or the calling namespace is. This is useful in cases where you need to find out at runtime in what context a given method was called.
Installation
The best way to install this package is through Composer:
Basic Usage
Setting Things Up
To create a new class that can fetch the caller's name or namespace, you can either extend the BrightNucleus\NamespaceBacktracer\NamespaceBacktracerClass
class or, should you already have a class you need to extend, you can import the BrightNucleus\NamespaceBacktracer\NamespaceBacktracerTrait
trait.
In both cases you'll want to override one or more of the three methods that allow you to adapt the behavior to your environment:
getIgnoredInterfaces()
This gets the list of interfaces/classes to ignore while traversing the backtrace.
getIgnoredFunctions()
This gets the list of functions to ignore while traversing the backtrace.
getGlobalNamespace()
This defines by what string the global namespace is represented.
Making The Call
To get the caller's namespace for a specific call, you pass the output of debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)
that was executed from within the callee to the getCallingNamespace()
method. If you don't provide a debug_backtrace
output, it will get fetched within the trait (which might be a different context than the one you want to check).
Example:
Contributing
All feedback / bug reports / pull requests are welcome.
License
Copyright (c) 2016 Alain Schlesser, Bright Nucleus
This code is licensed under the MIT License.