How to get namespace and class name in PHP from current class

Tip published on December 11, 2016 by @markvaneijk in PHP

Sometimes you just need this. One way to do this is by having to methods in your class, which returns the namespace and class name using the ReflectionClass functions in PHP:

<?php

public function namespace()
{
    return (new ReflectionClass($this))->getName();
}

public function className()
{
    return (new ReflectionClass($this))->getShortName();
}