loilo/traceback

Get the file or directory calling your code

1.0.0 2019-07-16 15:54 UTC

This package is auto-updated.

Last update: 2024-10-24 07:25:16 UTC


README

Traceback logo: two dog footprints next to each other

Traceback

Tests Version on packagist.org

Get the file or directory calling your code.

Installation

composer require loilo/traceback

Usage

/var/www/foo.php

require_once __DIR__ . '/vendor/autoload.php';

use Loilo\Traceback\Traceback;

function foo()
{
    var_dump(
        // File path calling foo()
        Traceback::file(),

        // Directory of file calling foo()
        Traceback::dir(),

        // File path calling foo() but skipping
        // the first file in the chain
        Traceback::file(1),

        // Directory of file calling foo() with skipping
        Traceback::dir(1)
    );
}

/var/www/bar.php

require_once __DIR__ . '/foo.php';

foo();

/var/www/baz/qux.php

require_once __DIR__ . '/../bar.php';

Running qux.php will output:

string(16) "/var/www/bar.php"
string(8) "/var/www"
string(20) "/var/www/baz/qux.php"
string(12) "/var/www/baz"