Yii2: PHP 7.2 get_class not accepting null

Created on 29 Jun 2017  路  5Comments  路  Source: yiisoft/yii2

What steps will reproduce the problem?

  • Use PHP 7.2
  • Use closure in Application::$bootstrap

What is the expected result?

No errors

What do you get instead?

yii\base\ErrorException get_class() expects parameter 1 to be object, null given

error-object-php72

Additional info

As of 7.2, http://php.net/manual/en/function.get-class.php:

Explicitly passing NULL as the object is no longer allowed as of PHP 7.2.0. The parameter is still optional and calling get_class() without a parameter from inside a class will work, but passing NULL now emits an E_WARNING notice.

| Q | A
| ---------------- | ---
| Yii version | All
| PHP version | 7.2 alpha|

php7 ready for adoption bug

Most helpful comment

After discussion with @dynasource the issue is about Closure support in Application::$bootstrap, which was not offically supported but worked because of Yii::createObject() supporting closures.

To fix this, we should add an instanceof Closure check to Application::bootstrap() and avoid calling get_class in that case.

@SamMousa returning a non-object in case where an object is expected the error message is normally very clear, i.e. "calling ... on a non-object" or "getting property from non-object". The error message in this case has a different cause.

All 5 comments

$component should never be null in that path.
Line 315 guarantees it is instantiated.
Could it be you have a component defined via a Closure that is not returning an object.

Side note: should \Yii::createObject() check the result of callable definitions to ensure that it is an object and not another type?

I have reviewed the usage of get_class() in the framework and I see no place where we rely on it to work with null.

Side note: should \Yii::createObject() check the result of callable definitions to ensure that it is an object and not another type?

that's unnecessary overhead imo.

@cebe, I think the overhead is negligible.
doing:
````
if (!is_object($result)) {
throw SomeException();

````
I think that's worth it. I'd rather have an exception thrown than get a hard to find error for some perceived performance gain...

After discussion with @dynasource the issue is about Closure support in Application::$bootstrap, which was not offically supported but worked because of Yii::createObject() supporting closures.

To fix this, we should add an instanceof Closure check to Application::bootstrap() and avoid calling get_class in that case.

@SamMousa returning a non-object in case where an object is expected the error message is normally very clear, i.e. "calling ... on a non-object" or "getting property from non-object". The error message in this case has a different cause.

@cebe There is another path that will throw the same error:

  1. Set bootstrap to ["test"].
  2. Set components to
    'components' => [ "test" => function() { return "not an object"; } ]

This will get the same error.

Was this page helpful?
0 / 5 - 0 ratings