Yii2: [BaseObject] why className static method deprecated?

Created on 30 Jun 2020  路  5Comments  路  Source: yiisoft/yii2

in doc

Deprecated since 2.0.14. On PHP >=5.5, use ::class instead.

i thinks ::class is not good response for some request.

for example ::class can not be used in dynamic class.

$myDynamicObject = new \yii\helpers\Html;
echo $myDynamicObject::Class; #PHP Compile Error > Cannot use ::class with dynamic class name

or

$myObjects = [new class1,new class2,new classN];
#very codes is here
foreach($myObjects as $obj)
    echo $myDynamicObject::Class; #PHP Compile Error > Cannot use ::class with dynamic class name
question

Most helpful comment

@ziaratban
Wait php 8.0
https://php.watch/versions/8.0#class-constant-on-objects

I'm don't understand - why get the className string from the className string.
Show your usage case please.

All 5 comments

Why do you need to?
The $myDynamicClass variable already has the class name in it.

@ziaratban
Wait php 8.0
https://php.watch/versions/8.0#class-constant-on-objects

I'm don't understand - why get the className string from the className string.
Show your usage case please.

@alex-code @darkdef
i edited my example and now is my answer is Wait php 8.0

when you call \yii\base\BaseObject::className(), the composer will load the class file.
\yii\base\BaseObject::class only return the a class name string "yii\base\BaseObject".
and, use XXX::class in code / config file is better for IDE like PHPStorm understand it.

Thank you for your question.
In order for this issue tracker to be effective, it should only contain bug reports and feature requests.

We advise you to use our community driven resources:

If you are confident that there is a bug in the framework, feel free to provide information on how to reproduce it. This issue will be closed for now.

_This is an automated comment, triggered by adding the label question._

Was this page helpful?
0 / 5 - 0 ratings