在PHP中访问数组中的受保护对象 [英] Access protected object in array in PHP
问题描述
我尝试访问以下内容并需要获取 [vid] 阵列单元格的值。
I am trying to access the following and need to get the value of [vid] array cell.
FieldCollectionItemEntity Object
(
[fieldInfo:protected] =>
[hostEntity:protected] => stdClass Object
(
**[vid]** => 119
[uid] => 1
[title] => My Page Name
[log] =>
[status] => 1
[comment] => 1
[promote] => 0
[sticky] => 0
[vuuid] => 3304d1cf-e3cf-4c5a-884a-4abb565ddced
[nid] => 119
[type] => subpage
[language] => und
[created] => 1408621327
[changed] => 1408640191
[tnid] => 0
[translate] => 0
[uuid] => 39145013-6637-4062-96e7-1b4589609c4f
[revision_timestamp] => 1408640191
我试过以下,但我想我没有这里的线索: / h2>
I tried the following, but I guess I don't have a clue from here:-
$mything = new myClass;
print $mything->accessObjectArray();
class myClass {
protected $var;
function accessObjectArray(){
return $this-> $var;
}
//other member functions
}
更新
我实际上只能访问具有以下多维数组的变量$ content。所有我想要的是获取数组单元格的值 [vid] 。
为此,我可以打印 $ content [field_image_title] [#object]
但之后它的受保护。这是我想知道如何访问这个数组。很遗憾,我无法访问 FieldCollectionItemEntity
以包含在我的网页中。
To do that, I could print $content["field_image_title"]["#object"]
but after that it's protected. That's where I am wondering that how can I access this array. I unfortunately do not have access FieldCollectionItemEntity
to include in my page.
输出: -
print_r($content);
Array
(
[field_image_title] => Array
(
[#theme] => field
[#weight] => 0
[#title] => Image Title
[#access] => 1
[#label_display] => hidden
[#view_mode] => full
[#language] => und
[#field_name] => field_image_title
[#field_type] => text
[#field_translatable] => 0
[#entity_type] => field_collection_item
[#bundle] => field_image_collection
[#object] => FieldCollectionItemEntity Object
(
[fieldInfo:protected] =>
[hostEntity:protected] => stdClass Object
(
[vid] => 119
[uid] => 1
[title] => My Page Name
[log] =>
[status] => 1
[comment] => 1
[promote] => 0
[sticky] => 0
[vuuid] => 3304d1cf-e3cf-4c5a-884a-4abb565ddced
[nid] => 119
[type] => subpage
[language] => und
[created] => 1408621327
[changed] => 1408640191
[tnid] => 0
[translate] => 0
[uuid] => 39145013-6637-4062-96e7-1b4589609c4f
[revision_timestamp] => 1408640191
[revision_uid] => 1
推荐答案
>
what your are doing with this:
return $this-> $var;
正在访问以 $ var
变量在定义的范围中不包含任何内容。传递它作为一个函数参数:
is accessing a property named after what is contained in your $var
variable which does not contain anything in the scope where it is defined. pass it as a function argument:
function accessObjectArray($var){
return $this-> $var;
}
print $mything->accessObjectArray('vid');
但是在任何情况下都不会工作,因为(如@MikeBrant提到)父对象属性中的对象。这样的东西可能会更好
but in any event, that won't work either since (as mentioned by @MikeBrant) you have an object in your parent object properties. something like this might work better
$o = new FieldCollectionItemEntity() // assumes this will construct the object in the state you have posted it
$o->accessObjectArray('hostEntity')->accessObjectArray('vid');
请注意方法 accessObjectArray($ var)
受保护属性的想法是防止你想要的实际发生。但! protected表示只有类及其扩展类可以访问值。创建自己的类扩展另一个:
the idea of a protected property is to prevent what you want to actually happen. But! protected means that only the class and it's extending classes can access a value. Make your own class that extends the other one:
class myClass extends FieldCollectionItemEntity {
function accessParentProtectedVars($var){
return $this->hostEntity->$var;
}
//other member functions
}
accessObjectArray()
函数将能够访问protected属性。请注意,它是硬编码以访问 hostEntity
对象。
then your accessObjectArray()
function will be able to acces the protected property. note that it's hardcoded to access the hostEntity
object.
但严重,您可能想咨询其他类,也许你会设计一种方法来最好地管理这个。如果我敢说,我提出的解决方案不是很好的做法。
but seriously, you may want to consult the creator of the other class and maybe you will devise a way to best manage this. My proposed solution is not that much of a good practice if I daresay.
这篇关于在PHP中访问数组中的受保护对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!