PHP: Is it possible to get the name of the class using the trait from within a trait static method?

php trait constructor
php trait implement interface
php get class name
php use trait in static method
traits in php w3schools
php trait override method
php can a trait use another trait
php use trait outside class

Can the name of a class using a trait be determined from within a static method belonging to that trait?

For example:

trait SomeAbility {
    public static function theClass(){
        return <name of class using the trait>;
    }
}

class SomeThing {
    use SomeAbility;
    ...
}

Get name of class:

$class_name = SomeThing::theClass();

My hunch is, probably not. I haven't been able to find anything that suggests otherwise.

Use late static binding with static:

trait SomeAbility {
    public static function theClass(){
        return static::class;
    }
}

class SomeThing {
    use SomeAbility;
}

class SomeOtherThing {
    use SomeAbility;
}

var_dump(
    SomeThing::theClass(),
    SomeOtherThing::theClass()
);

// string(9) "SomeThing"
// string(14) "SomeOtherThing"

https://3v4l.org/mfKYM

Traits - Manual, Traits ¶. As of PHP 5.4.0, PHP implements a method of code reuse called Traits. Multiple Traits can be inserted into a class by listing them in the use statement, The Aliased_Talker makes use of the as operator to be able to use B's bigTalk Unlike inheritance; if a trait has static properties, each class using that trait has​  Traits As of PHP 5.4.0, PHP implements a method of code reuse called Traits. Traits are a mechanism for code reuse in single inheritance languages such as PHP. A Trait is similar to a class, but only intended to group functionality in a fine-grained and consistent way.

Yep, using the get_called_class()

<?php
trait SomeAbility {
    public static function theClass(){
        return get_called_class();
    }
}

class SomeThing {
    use SomeAbility;
}
// Prints "SomeThing"
echo SomeThing::theClass();

PHP: How can I initialise a class that uses a trait from within a static , I have a bunch of unrelated classes that initialise with remote data. can I initialise a class that uses a trait from within a static method belonging to that same trait? to get the name of the class using the trait so that I can instantiate it. Edit: Of course, my first question should be, is it even possible in PHP? Traits is a mechanism for code reuse in single inheritance languages such as PHP. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies.

You can call get_class() without a parameter to get the name of the current class...

trait SomeAbility {
    public static function theClass(){
        return get_class();
    }
}

class SomeThing {
    use SomeAbility;
}

echo SomeThing::theClass().PHP_EOL;

Using Traits in PHP 5.4, A trait is similar to an abstract class which cannot be instantiated on its a static method getInstance() which creates an object of the class using this It's possible to introduce a new name for a method in a trait as an alias. If you call a static getInstance() function to create a instance of a class from another class, this function have to be static, if it is not static the original name of the caller class and not of the current class get returned. example: return x::getClassName(); return x::getStaticClassName();

Classes: Using A Trait, Hack's class model allows single inheritance with contracts being enforced In summary, a trait is a set of methods and/or state information that can be reused. a given name overrides one with the same name in any traits that class uses, which, In the case of a trait with a static property, each class using that trait has its  It is possible to write a completely self-contained Singleton base class in PHP 5.3 using the new get_called_class function. When called in a static method, this function returns the name of the class the call was made against.

PHP Objects, Patterns, and Practice, Note □ Where a method name clashes between traits it is not enough to alias one Using static methods in traits Most of the examples you have seen so far methods are declared in traits and accessed via the host class in the normal way. In PHP4, this function converts its return values to lowercase; but in PHP5, it leaves the return values in their original case. This can cause serious problems when trying to write code that dynamically calls a class method, and that works in both PHP4 and PHP5.

Traits vs. Interfaces in PHP, Traits can define both static members and static methods. It helps No two interface can be implemented by a particular class having same method name and  get_class_vars() will only return the properties that can be accessed from the current scope. 5.0.2 Calling get_class_vars() will now expose all the properties as an array, unlike previous behaviour where protected and private properties were prefixed with nul bytes.