mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 06:15:19 +08:00
Added yii\base\Object for backwards compatibility
fixes #7936 close #14478
This commit is contained in:
@ -486,8 +486,8 @@ class PhpDocController extends Controller
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$ref->isSubclassOf('yii\base\Object') && $className != 'yii\base\Object') {
|
||||
$this->stderr("[INFO] Skipping class $className as it is not a subclass of yii\\base\\Object.\n", Console::FG_BLUE, Console::BOLD);
|
||||
if (!$ref->isSubclassOf('yii\base\Object') && $className != 'yii\base\Object' && !$ref->isSubclassOf('yii\base\BaseObject') && $className != 'yii\base\BaseObject') {
|
||||
$this->stderr("[INFO] Skipping class $className as it is not a subclass of yii\\base\\BaseObject.\n", Console::FG_BLUE, Console::BOLD);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -23,13 +23,13 @@ echo DatePicker::widget([
|
||||
```
|
||||
Les propriétés de l'objet graphique sont faciles à écrire car la classe étend [[yii\base\Component]].
|
||||
|
||||
Tandis que les composants sont très puissants, ils sont un peu plus lourds que les objets normaux. Cela est dû au fait que, en particulier, la prise en charge des fonctionnalités [event](concept-events.md) et [behavior](concept-behaviors.md) requiert un peu plus de mémoire et de temps du processeur. Si vos composants n'ont pas besoin de ces deux fonctionnalités, vous devriez envisager d'étendre la classe [[yii\base\Object]] au lieu de la classe [[yii\base\Component]]. Ce faisant, votre composant sera aussi efficace que les objets PHP normaux, mais avec la prise en charge des [propriétés](concept-properties.md).
|
||||
Tandis que les composants sont très puissants, ils sont un peu plus lourds que les objets normaux. Cela est dû au fait que, en particulier, la prise en charge des fonctionnalités [event](concept-events.md) et [behavior](concept-behaviors.md) requiert un peu plus de mémoire et de temps du processeur. Si vos composants n'ont pas besoin de ces deux fonctionnalités, vous devriez envisager d'étendre la classe [[yii\base\BaseObject]] au lieu de la classe [[yii\base\Component]]. Ce faisant, votre composant sera aussi efficace que les objets PHP normaux, mais avec la prise en charge des [propriétés](concept-properties.md).
|
||||
|
||||
Lorsque votre classe étend la classe [[yii\base\Component]] ou [[yii\base\BaseObject]], il est recommandé que suiviez ces conventions :
|
||||
|
||||
- Si vous redéfinissez le constructeur, spécifiez un paramètre `$config` en tant que *dernier* paramètre du constructeur est passez le au constructeur du parent.
|
||||
- Appelez toujours le constructeur du parent *à la fin* de votre constructeur redéfini.
|
||||
- Si vous redéfinissez la méthode [[yii\base\Object::init()]], assurez-vous que vous appelez la méthode `init()` mise en œuvre par le parent *au début* de votre méthodes `init()`.
|
||||
- Si vous redéfinissez la méthode [[yii\base\BaseObject::init()]], assurez-vous que vous appelez la méthode `init()` mise en œuvre par le parent *au début* de votre méthodes `init()`.
|
||||
|
||||
Par exemple :
|
||||
|
||||
@ -38,9 +38,9 @@ Par exemple :
|
||||
|
||||
namespace yii\components\MyClass;
|
||||
|
||||
use yii\base\Object;
|
||||
use yii\base\BaseObject;
|
||||
|
||||
class MyClass extends Object
|
||||
class MyClass extends BaseObject
|
||||
{
|
||||
public $prop1;
|
||||
public $prop2;
|
||||
@ -80,7 +80,7 @@ La classe [[yii\base\BaseObject]] fait appliquer le cycle de vie suivant de l'ob
|
||||
|
||||
1. Pré-initialisation dans le constructeur. Vous pouvez définir les propriétés par défaut à cet endroit.
|
||||
2. Configuration de l'objet via `$config`. La configuration peut écraser les valeurs par défaut définies dans le constructeur.
|
||||
3. Post-initialisation dans la méthode [[yii\base\Object::init()|init()]]. Vous pouvez redéfinir cette méthode pour effectuer des tests sanitaires et normaliser les propriétés.
|
||||
3. Post-initialisation dans la méthode [[yii\base\BaseObject::init()|init()]]. Vous pouvez redéfinir cette méthode pour effectuer des tests sanitaires et normaliser les propriétés.
|
||||
4. Appel des méthodes de l'objet.
|
||||
|
||||
Les trois premières étapes arrivent toutes durant la construction de l'objet. Cela signifie qu'une fois que vous avez obtenu une instance de la classe (c.-à-d. un objet), cet objet a déjà été initialisé dans un état propre et fiable.
|
||||
|
@ -37,6 +37,8 @@ Yii Framework 2 Change Log
|
||||
- Enh #14188: Add constants and function for sysexits(3) to `ConsoleHelper` (tom--, samdark, cebe)
|
||||
- Bug #14165: Set `_slave` of `Connection` to `false` instead of `null` in `close` method (rossoneri)
|
||||
- Bug #14423: Fixed `ArrayHelper::merge` behavior with null values for integer-keyed elements (dmirogin)
|
||||
- Chg #7936: Deprecate `yii\base\Object` in favor of `yii\base\BaseObject` for compatibility with PHP 7.2 (rob006, cebe, klimov-paul)
|
||||
|
||||
|
||||
2.0.12 June 05, 2017
|
||||
--------------------
|
||||
|
@ -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`
|
||||
|
@ -10,7 +10,7 @@ namespace yii\base;
|
||||
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 @@ use Yii;
|
||||
*
|
||||
* 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 @@ use Yii;
|
||||
* 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 @@ use Yii;
|
||||
* 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
|
||||
{
|
||||
|
@ -13,7 +13,7 @@ use Yii;
|
||||
* 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
|
||||
|
30
framework/base/Object.php
Normal file
30
framework/base/Object.php
Normal file
@ -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
|
||||
{
|
||||
}
|
@ -40,6 +40,7 @@ return [
|
||||
'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',
|
||||
|
@ -36,7 +36,7 @@ use yii\helpers\ArrayHelper;
|
||||
* ```php
|
||||
* namespace app\models;
|
||||
*
|
||||
* use yii\base\Object;
|
||||
* use yii\base\BaseObject;
|
||||
* use yii\db\Connection;
|
||||
* use yii\di\Container;
|
||||
*
|
||||
@ -45,7 +45,7 @@ use yii\helpers\ArrayHelper;
|
||||
* function findUser();
|
||||
* }
|
||||
*
|
||||
* class UserFinder extends Object implements UserFinderInterface
|
||||
* class UserFinder extends BaseObject implements UserFinderInterface
|
||||
* {
|
||||
* public $db;
|
||||
*
|
||||
@ -60,7 +60,7 @@ use yii\helpers\ArrayHelper;
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* class UserLister extends Object
|
||||
* class UserLister extends BaseObject
|
||||
* {
|
||||
* public $finder;
|
||||
*
|
||||
|
21
tests/framework/base/BCObject.php
Normal file
21
tests/framework/base/BCObject.php
Normal file
@ -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;
|
||||
}
|
||||
}
|
@ -13,7 +13,7 @@ use yiiunit\TestCase;
|
||||
/**
|
||||
* @group base
|
||||
*/
|
||||
class ObjectTest extends TestCase
|
||||
class BaseObjectTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var NewObject
|
||||
@ -159,6 +159,19 @@ class ObjectTest extends TestCase
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user