【问题标题】:Accessing object's properties in an array php在数组php中访问对象的属性
【发布时间】:2014-11-20 03:49:14
【问题描述】:

我有一个会话变量$_SESSION["customer"],其中包含客户类的一个对象,该对象具有私有属性,例如名字、姓氏等。可以访问它们,但可以访问公共 getter 和 setter。我不知道如何访问它们并在我的代码中使用它们。

这是$_SESSION["customer"]的var_dump

object(Customer)#4 (13) { ["id":"Customer":private]=> string(4) "1019" ["fname":"Customer":private]=> string(4) "john" ["lname":"Customer":private]=> string(3) "doe" ["dob":"Customer":private]=> string(10) "1999-12-12" ["address1":"Customer":private]=> string(10) "humberwood" ["address2":"Customer":private]=> string(0) "" ["city":"Customer":private]=> string(7) "toronto" ["zip":"Customer":private]=> string(6) "m3a1c6" ["province":"Customer":private]=> string(2) "ON" ["gender":"Customer":private]=> string(1) "0" ["email":"Customer":private]=> string(21) "johndoe@humbermail.ca" ["username":"Customer":private]=> string(6) "humber" ["password":"Customer":private]=> string(6) "humber" } 

我尝试使用$username = $_SESSION["customer"]->Customer->getUsername();,但没有成功

【问题讨论】:

标签: php arrays session object


【解决方案1】:

你可以试试这个,因为使用 $_SESSION['customer'] 包含一个 Customer 对象。

$customer = $_SESSION['customer'];
$username = $customer->getUsername();

【讨论】:

  • 抛出错误Fatal error: main(): The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "Customer" of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition
  • 这一定意味着对象的内容是私有的。
  • 试试@Shashank。
  • 你能解释一下@kyle
  • 这只是意味着您的客户对象只不过是一个处于未完成状态的对象,同时存储在$_SESSION 中。参考这个:stackoverflow.com/questions/132194/…
猜你喜欢
  • 2018-11-12
  • 1970-01-01
  • 1970-01-01
  • 2014-10-10
  • 2015-08-03
  • 1970-01-01
  • 2016-10-28
  • 1970-01-01
相关资源
最近更新 更多