From d3407778cf8f6f41cfe2ee88ab253d98ea1b9ebb Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Tue, 18 Jul 2017 16:23:25 +0200 Subject: [PATCH] Added yii\base\Object for backwards compatibility issue #7936 --- framework/UPGRADE.md | 5 ++-- framework/base/BaseObject.php | 10 +++---- framework/base/Component.php | 2 +- framework/base/Object.php | 30 +++++++++++++++++++ framework/classes.php | 1 + framework/di/Container.php | 6 ++-- tests/framework/base/BCObject.php | 21 +++++++++++++ .../{ObjectTest.php => BaseObjectTest.php} | 15 +++++++++- 8 files changed, 78 insertions(+), 12 deletions(-) create mode 100644 framework/base/Object.php create mode 100644 tests/framework/base/BCObject.php rename tests/framework/base/{ObjectTest.php => BaseObjectTest.php} (90%) diff --git a/framework/UPGRADE.md b/framework/UPGRADE.md index e12ccf3f7f3..f07d146fab3 100644 --- a/framework/UPGRADE.md +++ b/framework/UPGRADE.md @@ -53,12 +53,13 @@ for both A and B. Upgrade from Yii 2.0.12 ----------------------- -* For compatibiliy with PHP 7.2 which does not allow classes to be named `Object` anymore, we needed to rename - `yii\base\Object` to `yii\base\BaseObject`. +* For compatibiliy with [PHP 7.2 which does not allow classes to be named `Object` anymore](https://wiki.php.net/rfc/object-typehint), + we needed to rename `yii\base\Object` to `yii\base\BaseObject`. `yii\base\Object` still exists for backwards compatibility and will be loaded if needed in projects that are running on PHP <7.2. The compatibility class `yii\base\Object` extends from `yii\base\BaseObject` so if you have classes that extend from `yii\base\Object` these would still work. + What does not work however will be code that relies on `instanceof` checks or `is_subclass_of()` calls for `yii\base\Object` on framework classes as these do not extend `yii\base\Object` anymore but only extend from `yii\base\BaseObject`. In general such a check is not needed as there is a `yii\base\Configurable` diff --git a/framework/base/BaseObject.php b/framework/base/BaseObject.php index 354470af9c8..714dd6340da 100644 --- a/framework/base/BaseObject.php +++ b/framework/base/BaseObject.php @@ -10,7 +10,7 @@ use Yii; /** - * Object is the base class that implements the *property* feature. + * BaseObject is the base class that implements the *property* feature. * * A property is defined by a getter method (e.g. `getLabel`), and/or a setter method (e.g. `setLabel`). For example, * the following getter and setter methods define a property named `label`: @@ -46,8 +46,8 @@ * * One can call [[hasProperty()]], [[canGetProperty()]] and/or [[canSetProperty()]] to check the existence of a property. * - * Besides the property feature, Object also introduces an important object initialization life cycle. In particular, - * creating an new instance of Object or its derived class will involve the following life cycles sequentially: + * Besides the property feature, BaseObject also introduces an important object initialization life cycle. In particular, + * creating an new instance of BaseObject or its derived class will involve the following life cycles sequentially: * * 1. the class constructor is invoked; * 2. object properties are initialized according to the given configuration; @@ -57,7 +57,7 @@ * you perform object initialization in the `init()` method because at that stage, the object configuration * is already applied. * - * In order to ensure the above life cycles, if a child class of Object needs to override the constructor, + * In order to ensure the above life cycles, if a child class of BaseObject needs to override the constructor, * it should be done like the following: * * ```php @@ -72,7 +72,7 @@ * of the constructor, and the parent implementation should be called at the end of the constructor. * * @author Qiang Xue - * @since 2.0 + * @since 2.0.13 */ class BaseObject implements Configurable { diff --git a/framework/base/Component.php b/framework/base/Component.php index 014a9e46e1d..e278f1720d1 100644 --- a/framework/base/Component.php +++ b/framework/base/Component.php @@ -13,7 +13,7 @@ * Component is the base class that implements the *property*, *event* and *behavior* features. * * Component provides the *event* and *behavior* features, in addition to the *property* feature which is implemented in - * its parent class [[\yii\base\Object|Object]]. + * its parent class [[\yii\base\BaseObject|BaseObject]]. * * Event is a way to "inject" custom code into existing code at certain places. For example, a comment object can trigger * an "add" event when the user adds a comment. We can write custom code and attach it to this event so that when the event diff --git a/framework/base/Object.php b/framework/base/Object.php new file mode 100644 index 00000000000..52e354ec387 --- /dev/null +++ b/framework/base/Object.php @@ -0,0 +1,30 @@ + + * @since 2.0 + * @deprecated since 2.0.13, the class name `Object` is invalid since PHP 7.2, use [[BaseObject]] instead. + * @see https://wiki.php.net/rfc/object-typehint + * @see https://github.com/yiisoft/yii2/issues/7936#issuecomment-315384669 + */ +class Object extends BaseObject +{ +} diff --git a/framework/classes.php b/framework/classes.php index ddd86cc75eb..1ad1ffaddf4 100644 --- a/framework/classes.php +++ b/framework/classes.php @@ -40,6 +40,7 @@ 'yii\base\Module' => YII2_PATH . '/base/Module.php', 'yii\base\NotSupportedException' => YII2_PATH . '/base/NotSupportedException.php', 'yii\base\Object' => YII2_PATH . '/base/Object.php', + 'yii\base\BaseObject' => YII2_PATH . '/base/BaseObject.php', 'yii\base\Request' => YII2_PATH . '/base/Request.php', 'yii\base\Response' => YII2_PATH . '/base/Response.php', 'yii\base\Security' => YII2_PATH . '/base/Security.php', diff --git a/framework/di/Container.php b/framework/di/Container.php index cdfa8676fb8..3461edcb24e 100644 --- a/framework/di/Container.php +++ b/framework/di/Container.php @@ -36,7 +36,7 @@ * ```php * namespace app\models; * - * use yii\base\Object; + * use yii\base\BaseObject; * use yii\db\Connection; * use yii\di\Container; * @@ -45,7 +45,7 @@ * function findUser(); * } * - * class UserFinder extends Object implements UserFinderInterface + * class UserFinder extends BaseObject implements UserFinderInterface * { * public $db; * @@ -60,7 +60,7 @@ * } * } * - * class UserLister extends Object + * class UserLister extends BaseObject * { * public $finder; * diff --git a/tests/framework/base/BCObject.php b/tests/framework/base/BCObject.php new file mode 100644 index 00000000000..7f916a71ebb --- /dev/null +++ b/tests/framework/base/BCObject.php @@ -0,0 +1,21 @@ +expectExceptionMessage('Getting write-only property: yiiunit\framework\base\NewObject::writeOnly'); $this->object->writeOnly; } + + public function testBackwardCompatibilityWithObject() + { + if (PHP_MAJOR_VERSION > 7 || (PHP_MAJOR_VERSION == 7 && PHP_MINOR_VERSION >= 2)) { + $this->markTestSkipped('This test is meant to run on PHP <7.2.0 to check BC with yii\base\Object'); + } + $this->assertInstanceOf('yii\base\Object', new BCObject()); + $this->assertInstanceOf('yii\base\BaseObject', new BCObject()); + + BCObject::$initCalled = false; + new BCObject(); + $this->assertTrue(BCObject::$initCalled); + } }