Yii2: Consider bringing ::className() back for 2.1

Created on 8 Oct 2016  路  5Comments  路  Source: yiisoft/yii2

The static yiibase\Object::className() method has been removed for Yii 2.1, because PHP 5.5 added the ::class keyword, which is generally a better way to go, since it's resolved at compile time rather than runtime, and doesn鈥檛 need to autoload the class.

In preparing Craft CMS for Yii 2.1 I鈥檝e come across two types of cases where ::className() would still be useful, though:

  1. If you are not sure if a $variable is an actual object or a class name string, and you just need to get its class name, it鈥檚 easier to call $variable::className() (which works either way), rather than something like is_object($variable) ? get_class($variable) : $variable.
  2. If you need to get an object鈥檚 class name from Twig, you鈥檇 need to define a custom Twig function to get it, rather than simply typing object.className().

Neither of these are deal-breakers, and maybe it鈥檚 still worth removing Object::className() because it will force people to start using ::class where it makes more sense, but I felt like it was worth bringing these up, just to point out that there are still cases where a static ::className() method makes sense.

Thoughts?

under discussion

Most helpful comment

Makes sense but there are cons as well. I strongly want to shorten the current inheritance tree one day and shift architecture overall towards composition approach.

All 5 comments

Arguments make sense.

Makes sense but there are cons as well. I strongly want to shorten the current inheritance tree one day and shift architecture overall towards composition approach.

If you are not sure if a $variable is an actual object or a class name string, and you just need to get its class name, it鈥檚 easier to call $variable::className() (which works either way), rather than something like is_object($variable) ? get_class($variable) : $variable.

how often is this the case? I'd prefer explicit code in this case. With

is_object($variable) ? get_class($variable) : $variable

it is directly clear what is going on.

get_class() is also the preferred way over a construct that calls a function and then checks back from which class it has been called, which would be ::className().

If you need to get an object鈥檚 class name from Twig, you鈥檇 need to define a custom Twig function to get it, rather than simply typing object.className().

this could be provided by yii2-twig by default, couldn't it?

I'd prefer explicit code in this case.

I agree with @cebe. Why would we keep a function that will be rarely used and if it used, for obscure usecases. Explicit code will then be much more useful. Additionally, cleaning up the inheritance tree is a good thing. The less functions in base objects like Object / Component, the better. Lean is mean!

@cebe

this could be provided by yii2-twig by default, couldn't it?

Eh, seems like a weird place to put it, since it鈥檚 not something most apps would need. (And we鈥檙e not even using that extension due to some special requirements.)

Sounds like there鈥檚 some consensus that the function should stay gone. I鈥檓 fine with that and will close the issue.

Was this page helpful?
0 / 5 - 0 ratings