Skip to content

Instantly share code, notes, and snippets.

@xicond
Created May 25, 2018 07:50
Show Gist options
  • Save xicond/7012817a76c5c01cffeee4a300e0cdd6 to your computer and use it in GitHub Desktop.
Save xicond/7012817a76c5c01cffeee4a300e0cdd6 to your computer and use it in GitHub Desktop.
Test Arrayable to output readonly
<?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