Skip to content

Commit

Permalink
Rewrite Model::attributes() (#19309)
Browse files Browse the repository at this point in the history
* Optimize Model::attributes()

* Update CHANGELOG.md
  • Loading branch information
WinterSilence committed Mar 18, 2022
1 parent a843c61 commit 7f24479
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 10 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Yii Framework 2 Change Log
- Bug #19243: Handle `finfo_open` for tar.xz as `application/octet-stream` on PHP 8.1 (longthanhtran)
- Bug #19235: Fix return type compatibility of `yii\web\SessionIterator` class methods for PHP 8.1 (virtual-designer)
- Bug #19291: Reset errors and validators in `yii\base\Model::__clone()` (WinterSilence)
- Enh #19309: Optimize `yii\base\Model::attributes()` (WinterSilence)


2.0.45 February 11, 2022
Expand Down
14 changes: 4 additions & 10 deletions framework/base/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,21 +260,15 @@ public function formName()

/**
* Returns the list of attribute names.
*
* By default, this method returns all public non-static properties of the class.
* You may override this method to change the default behavior.
* @return array list of attribute names.
*
* @return string[] list of attribute names.
*/
public function attributes()
{
$class = new ReflectionClass($this);
$names = [];
foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
if (!$property->isStatic()) {
$names[] = $property->getName();
}
}

return $names;
return array_keys(Yii::getObjectVars($this));
}

/**
Expand Down

0 comments on commit 7f24479

Please sign in to comment.