在数组php中访问对象的属性

时间:2014-11-20 03:49:14

标签: php arrays session object

我有一个会话变量$_SESSION["customer"],它包含一个客户类对象,它具有私有属性,如firstname,lastname等。他们可以访问,但公共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();,但徒劳无功

1 个答案:

答案 0 :(得分:0)

您可以尝试这样做,因为使用$_SESSION['customer']包含Customer对象。

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