Return Component Id From Within A Component

Currently it appears theres no way to get the id of the component from within the component without looping over components like so:


$componentName = null;

foreach (Yii::$app->components as $id=>$component)

{

    if (!isset($component['class'])) continue;


    if ($this instanceof $component['class'])

    {

        $componentName = $id;

        break;

    }

}

Can we add this functionality unless I’ve missed something and its in there already?

Thanks

Actually not faced this. Normally I start with a component id to fetch its class and properties and not vice versa.

BTW - in your code, you may face an error when you have one component id class extending from another component class.

Example:

In your config




'components' => [

   'comp1' => ['class'=> 'path/to/Component1'],

   'comp2' => ['class'=> 'path/to/Component2'],

]



And your class definitions:




class Component1 extends Component {

}


class Component2 extends Component1 {

   // Your code may wrongly return `comp1` as id since 

   // Component2 is an instanceof Component1

}