3

I have an object that is being thrown into the session array, and I want to run a foreach on the items property.

I can't seem to access it. I see that it's private, but I can't help but wonder why var_dump can show me what the property contains yet I can't read the data as it throws a fatal error?

I suppose I could do some output buffering and evaluate var_dump as a string if I really have to like this but it seems like there should be a better method. Any ideas how I can access _items?

The object code var_dumped from var_dump($_SESSION['PHPurchaseCart']):

object(PHPurchaseCart)#191 (4) {
  ["_items:private"]=>
  array(2) {
    [0]=>
    object(PHPurchaseCartItem)#190 (6) {
      ["_productId:private"]=>
      string(2) "80"
      ["_quantity:private"]=>
      int(1)
      ["_optionInfo:private"]=>
      string(20) "Monthly Sponsorship "
      ["_priceDifference:private"]=>
      string(3) ".01"
      ["_customFieldInfo:private"]=>
      NULL
      ["_formEntryIds:private"]=>
      array(0) {
      }
    }
    [1]=>
    object(PHPurchaseCartItem)#189 (6) {
      ["_productId:private"]=>
      string(2) "75"
      ["_quantity:private"]=>
      int(1)
      ["_optionInfo:private"]=>
      string(20) "Monthly Sponsorship "
      ["_priceDifference:private"]=>
      string(3) ".02"
      ["_customFieldInfo:private"]=>
      NULL
      ["_formEntryIds:private"]=>
      array(0) {
      }
    }
  }
  ["_promotion:private"]=>
  NULL
  ["_promoStatus:private"]=>
  int(0)
  ["_shippingMethodId:private"]=>
  NULL
}

Ways I've tried to access it:

$fun = $_SESSION['PHPurchaseCart'];
var_dump($fun->_items);
exit;

The above throws a fatal error.

Community
  • 1
  • 1
Rooster
  • 9,954
  • 8
  • 44
  • 71

3 Answers3

6

That's the idea of private properties: You can't access them. You should really not break this concept. If you really want to access such property, mark is as "public" in the original class definition.

The reason why var_dump can access it is because it is an internal function, and it has the "power" to view the whole object. However, your code doesn't have that power.

I wouldn't recommend it, but if you really need to access a private property, you can use PHP Reflection to achieve it.

kuba
  • 7,329
  • 1
  • 36
  • 41
  • actually, I didn't write the code for that object, but I used a get_class_methods on it and found an internal function to give me what I need, which is basically what you wanted me to add (: oooo! I didn't know about reflection. Thanks for pointing me towards it! – Rooster Apr 20 '12 at 16:17
  • you can break it in a horribly hacky way: ```var_dump(((array)$o)["\x00".get_class($o)."\x00"."PrivatePropertyName"]);``` will give you access to p->PrivatePropertyName: https://3v4l.org/7WKoA – hanshenrik Sep 28 '22 at 12:47
1

Private properties that are needed by external code usually have a public method to read them, in this case it's getItems().

$items = $_SESSION['PHPurchaseCart']->getItems();
MrCode
  • 63,975
  • 10
  • 90
  • 112
  • yeah, I did a get_class_methods on it after posting to find that. But I was still curious about the var_dump thing. Reflection seems intriguing. – Rooster Apr 20 '12 at 16:18
0

I see that it's private, but I can't help but wonder why var_dump can show me what the property contains yet I can't read the data as it throws a fatal error?

Please understand you should use getter and setter methods as explained in the chosen answer.

But for completion on why and how it can be read (could help in debugging). Well, the value is there but private methods are surrounded by NULL bytes (ASCII Value 0).

So if you really want to see that value with a var_dump();

$key = "\0_items\0";
var_dump($fun->$key);
rovr138
  • 695
  • 6
  • 9