Get the plugin name from within plugin code?

Hi. Is there a way to retrieve a plugin’s own name from within the plugin code, e.g. by accessing the Plugin object of the plugin itself?

Problem at hand: I have a class within a plugin and would like to get the plugin’s name as a string ($plugin->name() or equivalent).

Thank you :slight_smile:

Actually, I must admit I might not understand your question. What else beside of $plugin->name() do you need?

Sorry if that was not clear enough…

Calling $plugin in a class within a plugin leads - not unexpectedly - to an “Undefined variable: plugin” error. So I’ll have to assign the plugin object to that variable first, in order to then retrieve the name using $plugin->name().

I am after the means to access that object. In other words: $plugin = ...? kirby()->plugin() does not work as it expects a plugin name property, and kirby()->plugins() gives me all plugins, but I can’t determine which one is the one that the current class is part of.

I am still lost - but that might be due I am coming from a general perspective. However I would guess, if you are inside a plugins class which is extended from the general Plugin class, $this->name() should work?

I don’t think this is possible unless you register a method in your class or so that returns the plugin name or if you add a composer.json, parse it and fetch the name from there in the same way that Kirby does it.

Thank you both! I already had a hunch this wouldn’t work …but your comments made me aware I was over-complicating the task at hand; the idea of parsing composer.json is actually a promising approach.