Skip to content

Commit

Permalink
Added yii\base\Object for backwards compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
cebe committed Jul 18, 2017
1 parent 8ed4fac commit d340777
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 12 deletions.
5 changes: 3 additions & 2 deletions framework/UPGRADE.md
Expand Up @@ -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`
Expand Down
10 changes: 5 additions & 5 deletions framework/base/BaseObject.php
Expand Up @@ -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`:
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -72,7 +72,7 @@
* of the constructor, and the parent implementation should be called at the end of the constructor.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
* @since 2.0.13
*/
class BaseObject implements Configurable
{
Expand Down
2 changes: 1 addition & 1 deletion framework/base/Component.php
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions framework/base/Object.php
@@ -0,0 +1,30 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/

namespace yii\base;

use Yii;

/**
* Object is the base class that implements the *property* feature.
*
* It has been replaced by [[BaseObject]] in version 2.0.13 because `object` has become a reserved word which can not be
* used as class name in PHP 7.2.
*
* Please refer to [[BaseObject]] for detailed documentation and to the
* [UPGRADE notes](https://github.com/yiisoft/yii2/blob/2.0.13/framework/UPGRADE.md#upgrade-from-yii-2012)
* on how to migrate your application to use [[BaseObject]] class to make your application compatible with PHP 7.2.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @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
{
}
1 change: 1 addition & 0 deletions framework/classes.php
Expand Up @@ -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',
Expand Down
6 changes: 3 additions & 3 deletions framework/di/Container.php
Expand Up @@ -36,7 +36,7 @@
* ```php
* namespace app\models;
*
* use yii\base\Object;
* use yii\base\BaseObject;
* use yii\db\Connection;
* use yii\di\Container;
*
Expand All @@ -45,7 +45,7 @@
* function findUser();
* }
*
* class UserFinder extends Object implements UserFinderInterface
* class UserFinder extends BaseObject implements UserFinderInterface
* {
* public $db;
*
Expand All @@ -60,7 +60,7 @@
* }
* }
*
* class UserLister extends Object
* class UserLister extends BaseObject
* {
* public $finder;
*
Expand Down
21 changes: 21 additions & 0 deletions tests/framework/base/BCObject.php
@@ -0,0 +1,21 @@
<?php

namespace yiiunit\framework\base;


use yii\base\Object;

class BCObject extends \yii\base\Object
{
public static $initCalled = false;

public function __construct($config = [])
{
Object::__construct($config);
}

public function init()
{
static::$initCalled = true;
}
}
Expand Up @@ -13,7 +13,7 @@
/**
* @group base
*/
class ObjectTest extends TestCase
class BaseObjectTest extends TestCase
{
/**
* @var NewObject
Expand Down Expand Up @@ -159,6 +159,19 @@ public function testReadingWriteOnlyProperty()
$this->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);
}
}


Expand Down

0 comments on commit d340777

Please sign in to comment.