PropertyAccess组件
编辑该页面警告:你浏览的文档欧宝官网下载appob娱乐下载Symfony 2.2,不再维护。
读这个页面的更新版本Symfob娱乐下载ony 6.2(当前的稳定版本)。
PropertyAccess组件
PropertyAccess组件提供函数来读取和写入从/到一个对象使用一个简单的字符串或数组表示法。
2.2
新2.2 Symfony PropertyAccess组件。ob娱乐下载在此之前,PropertyPath
类位于表单组件。
安装
你可以安装组件在两种不同的方式:
- 安装它通过作曲家(
ob娱乐下载symfony /属性访问
在Packagist); - 使用官方的Git存储库(https://github.com/ob娱乐下载symfony/PropertyAccess)。
使用
这个组件的入口点PropertyAccess: getPropertyAccessor工厂。这家工厂将创建一个新的实例PropertyAccessor类的默认配置:
1 2 3
使用ob娱乐下载\组件\PropertyAccess\PropertyAccess;美元访问器= PropertyAccess::getPropertyAccessor ();
读取数组
你可以阅读的数组PropertyAccessor: getValue方法。这是通过使用索引在PHP中使用的符号:
1 2 3 4 5 6 7
/ /……美元人=数组(“first_name”= >“Wouter”,);回声美元访问器- >getValue (美元人,“[first_name]”);/ /“Wouter”回声美元访问器- >getValue (美元人,“[时代]”);/ /空
正如您可以看到的,该方法将返回零
如果索引不存在。
您还可以使用多维数组:
1 2 3 4 5 6 7 8 9 10 11 12
/ /……美元人=数组(数组(“first_name”= >“Wouter”),数组(“first_name”= >“瑞恩”));回声美元访问器- >getValue (美元人,“[0][first_name]”);/ /“Wouter”回声美元访问器- >getValue (美元人,“[1][first_name]”);/ /“瑞恩”
阅读对象
的getValue
方法是一个非常健壮的方法,你可以看到它的所有功能在处理对象。
访问公共属性
读取属性,使用“点”符号:
1 2 3 4 5 6 7 8 9 10 11
/ /……美元人=新人();美元人- >firstName =“Wouter”;回声美元访问器- >getValue (美元人,“firstName”);/ /“Wouter”美元孩子=新人();美元孩子- >firstName =“酒吧”;美元人- >孩子=数组(美元孩子);回声美元访问器- >getValue (美元人,的孩子[0].firstName”);/ / '酒吧'
谨慎
访问使用的公共属性是最后的选择PropertyAccessor
。它试图访问下面的值使用方法之前,首先直接使用属性。例如,如果你有一个公共的属性的getter方法,它将使用getter。
使用getter
的getValue
使用getter方法还支持阅读。该方法将使用常见的命名约定来创建getter。它camelizes属性名(first_name
就变成了FirstName
)和前缀得到
。所以实际的方法getFirstName
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/ /……类人{私人美元firstName=“Wouter”;公共函数getFirstName(){返回美元这- >firstName;}}美元人=新人();回声美元访问器- >getValue (美元人,“first_name”);/ /“Wouter”
使用Hassers /伊塞
它甚至不停止。如果没有getter发现,访问器将寻找一个伊塞或拥有。该方法使用相同的方式创建getter,这意味着您可以这样做:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21日22日23日24日25
/ /……类人{私人美元作者=真正的;私人美元孩子们=数组();公共函数isAuthor(){返回美元这- >作者;}公共函数hasChildren(){返回0! = = count (美元这- >儿童);}}美元人=新人();如果(美元访问器- >getValue (美元人,“作者”)){回声他是一个作家的;}如果(美元访问器- >getValue (美元人,“孩子”)){回声“他有孩子”;}
这将会产生:他是一个作家
魔术方法
最后,getValue
可以使用魔法__get
方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/ /……类人{私人美元孩子们=数组(“Wouter”= >数组(…),);公共函数__get(美元id){返回美元这- >孩子(美元id];}}美元人=新人();回声美元访问器- >getValue (美元人,“Wouter”);/ /数组(…)
写入数组
的PropertyAccessor
类可以做的不仅仅是读一个数组,数组也可以写。这可以通过使用PropertyAccessor:: setValue方法:
1 2 3 4 5 6 7 8
/ /……美元人=数组();美元访问器- >setValue (美元人,“[first_name]”,“Wouter”);回声美元访问器- >getValue (美元人,“[first_name]”);/ /“Wouter”/ /或/ / echo $人(“first_name”);/ /“Wouter”
写作对象
的setValue
方法有相同的特性getValue
方法。您可以使用setter,魔法__set
方法或属性设置值:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17日18 19 20 21日22日23日24日25日26日27 28 29
/ /……类人{公共美元firstName;私人美元姓;私人美元孩子们=数组();公共函数setLastName(美元的名字){美元这- >lastName =美元的名字;}公共函数__set(美元财产,美元价值){美元这- >美元属性=美元价值;}/ /……}美元人=新人();美元访问器- >setValue (美元人,“firstName”,“Wouter”);美元访问器- >setValue (美元人,“姓”,”德容);美元访问器- >setValue (美元人,“孩子”,数组(新人()));回声美元人- >firstName;/ /“Wouter”回声美元人- >getLastName ();/ /“德容”回声美元人- >孩子;/ /数组(人());
混合对象和数组
你也可以混合对象和数组:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17日18 19 20 21日22日23日24日25日26日27
/ /……类人{公共美元firstName;私人美元孩子们=数组();公共函数setChildren(美元孩子们){返回美元这- >孩子;}公共函数调用getChildren(){返回美元这- >孩子;}}美元人=新人();美元访问器- >setValue (美元人,“孩子[0]”,新人);/ / = $人- >调用getChildren()[0] =新人()美元访问器- >setValue (美元人,的孩子[0].firstName”,“Wouter”);/ / = $人- >调用getChildren () [0] - > firstName =“Wouter”回声“你好”。美元访问器- >getValue (美元人,的孩子[0].firstName”);/ /“Wouter”/ / = $人- >调用getChildren () [0] - > firstName