class Foo { public function getClassName() { return self::class; } } $obj = new Foo(); echo $obj->getClassName(); // Output: "Foo"
trait Bar { public function getTraitName() { return self::class; } } class Baz { use Bar; } $obj = new Baz(); echo $obj->getTraitName(); // Output: "Baz"In this example, we define a trait `Bar` with a method `getTraitName` that returns the name of the current trait using `self::class`. We then use the `use` keyword to include the trait in the class `Baz` and instantiate `Baz` to call the `getTraitName` method. Package/Library: PHP Standard Library (included in PHP core)