Created
May 25, 2018 07:50
-
-
Save xicond/7012817a76c5c01cffeee4a300e0cdd6 to your computer and use it in GitHub Desktop.
Test Arrayable to output readonly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use yii\base\Arrayable; | |
use yii\base\ArrayableTrait; | |
use yii\base\Component; | |
use yii\helpers\ArrayHelper; | |
class News extends Component implements Arrayable | |
{ | |
use ArrayableTrait; | |
protected $article_title = ''; | |
public function setTitle($value) | |
{ | |
$this->article_title = strip_tags(htmlspecialchars_decode($value)); | |
return $this; | |
} | |
public function getTitle() : string | |
{ | |
return $this->article_title; | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function fields() | |
{ | |
$fields = array_merge(array_keys(\Yii::getObjectVars($this)), ['title']); | |
return array_combine($fields, $fields); | |
} | |
} | |
$news = new Article; | |
$news->title = 'Test'; | |
var_dump(ArrayHelper::toArray($news)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment