【发布时间】:2016-07-03 19:57:43
【问题描述】:
请有人向我解释一下这段代码:
<?php
class Model
{
protected $dates = [];
public function __get($property)
{
if (in_array($property, $this->dates)) {
return new DateTime($this->{$property});
}
return $this->{$property};
}
}
class User extends Model
{
protected $dates = ['createdAt'];
protected $createdAt = '2016-01-01 12:35:15';
}
class Comment extends Model
{
protected $dates = ['createdAt'];
protected $createdAt = '2016-01-01 12:35:15';
}
$comment = new Comment;
var_dump($comment->createdAt->format('H:i'));
我不明白他是如何在这里使用数组的。他是否仅使用 $property 访问数组的索引? $this->{$property} 是如何工作的?
好笑,不过这段代码我看不懂,虽然只是初学者的例子……
亲切的问候, 米兰
【问题讨论】:
-
我投票决定将此问题作为离题结束,因为Stack Overflow 不接受要求解释代码的问题
标签: php arrays properties