简体   繁体   English

如何从对象数组PHP获取对象的属性值

[英]How to get an object's attribute value from an array of objects PHP

my problem is in PHP.. I have an object location with attributes name,date,address and get and set methods... class location{ public $name; 我的问题是在PHP中。我有一个对象位置,该对象位置具有属性名称,日期,地址以及获取和设置方法...类位置{public $ name; public $date; 公开日期 public $address; 公共地址; public function set_name($myname){ $this->name=$myname; 公共功能set_name($ myname){$ this-> name = $ myname; } public function get_name(){ return $this->name; }公共函数get_name(){返回$ this-> name; } } Then i created an array $locations_array=array(); 然后我创建了一个数组$locations_array=array();

i also created an instance of the class location: 我还创建了一个类位置的实例:

$my_loc=new location;
$my_loc->set_name("africa");
$locations_array[0]=$my_loc;
echo $locations_array[0]->get_name();

when i try to echo the name of the object $my_loc..there's an error..how can i remedy this? 当我尝试回显对象$ my_loc的名称时。出现错误。我该如何解决?

I tried this in an interactive PHP shell and everything worked fine. 我在交互式PHP Shell中进行了尝试,一切正常。 It must be some kind of error specific to your computer, either that or a typo. 它一定是您的计算机所特有的某种错误,无论是错误还是错字。

And you might want to reconsider even the existence of the set_name and get_name functions if the name property is public anyway. 而且,如果name属性仍然是公共的,则可能甚至要重新考虑set_name和get_name函数的存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM