Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the trait name with namespace inside itself?

I want to know if there is a way to get the traits namespace inside itself, I know that I can use self::class to get the classname, but inside a trait it gets the namespace of the class that is using the trait, I don't want to type it's name fixed like new ReflectionClass('trait')

Is there any function or const that can do this?

like image 340
Matheus Naldi Avatar asked Feb 08 '18 17:02

Matheus Naldi


People also ask

How do you call a trait in controller?

You can make a function to and call the trait function. Show activity on this post. Now, you can call trait functions with $this->functionName () in functions.

What does namespace do php?

Namespaces are qualifiers that solve two different problems: They allow for better organization by grouping classes that work together to perform a task. They allow the same name to be used for more than one class.

What are namespaces in laravel?

Namespaces in Laravel are defined as a class of elements in which each element has a different name to that associated class. The use keyword allows us to shorten the namespace. Actually, it is pretty easy to use namespaces.

How to get the name of a class without its namespace?

Using the CLASS constant generally works, however it fails if used in a child class of a class which uses a trait: class_basename($objectOrString); ? class_basename ($objectOrString); ? You can do this with reflection. You can use ReflectionClass::getShortName which get name of the class without its namespace.

How do you use the same namespace inside two different namespaces?

This same behavior (same as inline namespaces) can also be achieved by using the “using” declarative inside namespaces. A using-directive that names the inline namespace is implicitly inserted in the enclosing namespace (similar to the implicit using-directive for the unnamed namespace).

How to resolve naming conflicts between traits in a class?

To resolve naming conflicts between Traits used in the same class, the insteadof operator needs to be used to choose exactly one of the conflicting methods. Since this only allows one to exclude methods, the as operator can be used to add an alias to one of the methods.

What is a built-in namespace in Python?

Built-in Namespace in Python 1 The Built-in namespace in Python is created when the python interpreter is started and exists until the interpreter runs. 2 This namespace covers the built-in functions, a built-in namespace contains the built-in functions like print (), open... More ...


1 Answers

I'm a little bit confused by your question but if you need the fully qualified name from the trait then you may use __TRAIT__ magic constant and if you only need the namespace of the trait then you may use __NAMESPACE__. For example, declare a trait using a namespace:

namespace App\Http\Controllers\Traits;

trait Methods
{
    public function getNamespace()
    {
        // Get fully qualified name of the trait
        echo __TRAIT__; // App\Http\Controllers\Traits\Methods

        echo PHP_EOL;

        // Get namespace of the trait
        echo __NAMESPACE__; // App\Http\Controllers\Traits
    }
}

Now, declare a class using another namespace and use that trait inside this class:

namespace App\Http\Controllers;

use App\Http\Controllers\Traits\Methods;

class TraitController
{
    use Methods;

    public function index()
    {
        // Call the method declared in trait
        $this->getNamespace();
    }
}


(new TraitController)->index();

The predefined magic constants __TRAIT__ (since 5.4.0) and __NAMESPACE__ (since 5.3.0) is used so use which one is needed. Tested in php v-5.4.0. Check the demo here.

Also, if you want to get the fully qualified name of the trait from the class that is using it then you may use NameOfTheTrait::class (NameOfTheClass::class/NameOfTheInterface::class) but this is available since php v-5.5.

Also be careful when using self::class. The self::class will give the fully qualified name of the class where you've used it because the self always references the lexical scope (where it's physically used) since the scope of self is determined during the compile time so you may get unexpected results if you inherit a class where a self::class statement is used. In other words, if you call any static method from a child class then the calling context will be still the parent class if you use self in your parent class, in that case you need to use static instead of self. This is actually another topic so please read more on php manual about Late Static Binding.

like image 108
The Alpha Avatar answered Sep 21 '22 04:09

The Alpha