Home > Backend Development > PHP Tutorial > oop - PHP有段写法不懂意思,求指教

oop - PHP有段写法不懂意思,求指教

WBOY
Release: 2016-06-06 20:41:33
Original
1035 people have browsed it

今天看laravel框架代码一个类中有一段代码

<code>public function isLocal()
{
    return $this['env'] == 'local';
}
</code>
Copy after login
Copy after login

从来没见过这样的写法$this['env']这样有什么作用?

回复内容:

今天看laravel框架代码一个类中有一段代码

<code>public function isLocal()
{
    return $this['env'] == 'local';
}
</code>
Copy after login
Copy after login

从来没见过这样的写法$this['env']这样有什么作用?

Application类继承了Container 类 Container类实现了ArrayAccess,
所以Container的实例是支持数组式访问的: $object['key'];
数组式访问在Container里的实现:https://github.com/laravel/framework/blob/4.2/src/Illuminate/Container/Container.php#L814行到末尾
PHP的数组式访问接口:http://php.net/manual/zh/class.arrayaccess.php

@安正超 为你解答 => http://wenda.golaravel.com/question/5

是不是有一个名为__get的方法譬如:

<code>public function __get($key) {
    return $this->var[$key];
}
</code>
Copy after login

这是PHP类集合的实现 具体看demo http://blog.sina.com.cn/s/blog_7550abf30101ajda.html

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template