PHPackages                             brightnucleus/namespace-backtracer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. brightnucleus/namespace-backtracer

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

brightnucleus/namespace-backtracer
==================================

Get the namespace of the calling object.

v0.1.1(10y ago)232MIT

Since Jun 18Compare

[ Source](https://github.com/brightnucleus/namespace-backtracer)[ Packagist](https://packagist.org/packages/brightnucleus/namespace-backtracer)[ RSS](/packages/brightnucleus-namespace-backtracer/feed)WikiDiscussions Synced today

READMEChangelogDependencies (1)Versions (3)Used By (0)

Bright Nucleus Namespace Backtracer
===================================

[](#bright-nucleus-namespace-backtracer)

[![Scrutinizer Code Quality](https://camo.githubusercontent.com/4d5b496c443b68cbda232d629df971ad527260fcd16e6ceb0f97065a63200695/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6272696768746e75636c6575732f6e616d6573706163652d6261636b7472616365722e737667)](https://scrutinizer-ci.com/g/brightnucleus/namespace-backtracer/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/a6b1f234eed197a6aa7a7f580d89be9431ceef4433109bbe409b44c3737ab991/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f6272696768746e75636c6575732f6e616d6573706163652d6261636b7472616365722e737667)](https://scrutinizer-ci.com/g/brightnucleus/namespace-backtracer/?branch=master)[![Build Status](https://camo.githubusercontent.com/fc614f9ae51e0c98b2a00bbcf58d07abb6100408a39f168f69321700e7d92948/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f6275696c642f672f6272696768746e75636c6575732f6e616d6573706163652d6261636b7472616365722e737667)](https://scrutinizer-ci.com/g/brightnucleus/namespace-backtracer/build-status/master)[![Codacy Badge](https://camo.githubusercontent.com/37e6321cfa7f99333d4e06f0b5e50c3f780c8590115a84dab9ad9e28001a9de5/68747470733a2f2f696d672e736869656c64732e696f2f636f646163792f66366362656535613161646234653239393561613565343264383533316663652e737667)](https://www.codacy.com/app/BrightNucleus/namespace-backtracer)[![Code Climate](https://camo.githubusercontent.com/93ec7938ff4d196e0659fb19a4a81cbc706ac4fa653a6c8e613647b39cd4ef3b/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636c696d6174652f6769746875622f6272696768746e75636c6575732f6e616d6573706163652d6261636b7472616365722e737667)](https://codeclimate.com/github/brightnucleus/namespace-backtracer)

[![Latest Stable Version](https://camo.githubusercontent.com/6378ab892978f9cc6b4cb0b8382fadbbecae9b8a710c2c015bd4edc94a55a065/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6272696768746e75636c6575732f6e616d6573706163652d6261636b7472616365722e737667)](https://packagist.org/packages/brightnucleus/namespace-backtracer)[![Total Downloads](https://camo.githubusercontent.com/f3c9b7831d1a2a325b1952c024b6d16cc9dbb5440562b91cdc12db07d81fc050/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6272696768746e75636c6575732f6e616d6573706163652d6261636b7472616365722e737667)](https://packagist.org/packages/brightnucleus/namespace-backtracer)[![Latest Unstable Version](https://camo.githubusercontent.com/e130556fecf8376637e7abea1eceb308e3256ee2f7ae477fc6ee4ad8beb3f3fb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f767072652f6272696768746e75636c6575732f6e616d6573706163652d6261636b7472616365722e737667)](https://packagist.org/packages/brightnucleus/namespace-backtracer)[![License](https://camo.githubusercontent.com/60f659b514821e5802fc9a42614c66bf2fe45c238fef2636c2b1f752c42d8ef0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6272696768746e75636c6575732f6e616d6573706163652d6261636b7472616365722e737667)](https://packagist.org/packages/brightnucleus/namespace-backtracer)

Get the namespace of the calling object, by scanning the debug backtrace, skipping a known set of namespaces &amp; functions in the process.

Table Of Contents
-----------------

[](#table-of-contents)

- [Overview](#overview)
- [Installation](#installation)
- [Basic Usage](#basic-usage)
    - [Setting Things Up](#setting-things-up)
    - [Making The Call](#making-the-call)
- [Contributing](#contributing)
- [License](#license)

Overview
--------

[](#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
------------

[](#installation)

The best way to install this package is through Composer:

```
composer require brightnucleus/namespace-backtracer
```

Basic Usage
-----------

[](#basic-usage)

### Setting Things Up

[](#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

[](#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:**

```
