1

What is the name of the integer between brackets in a var_dump of an object. And how do I acces it with PHP?

I'm referring to the (3) in the next example.

    object(SimpleXMLElement)#18 (3) {
       ["ID"]=>
      string(3) "xx"
       ["Name"]=>
       string(25) "xx"
       ["Date"]=>
       string(10) "xx"
    }

3 Answers 3

2

this is the number of properties of an object. to count this, you can cast your object to an array and use count():

$number = count((array)$object);

EDIT: i did a small test (see at codepad) wich prooves that casting to an array is what you want to do instead of using get_object_vars() as others mentioned because the later one doesn't count private properties while array-casting as well as var_dump do count these.

Sign up to request clarification or add additional context in comments.

Comments

0

It's the number of public properties of that object, and isn't directly accessible

4 Comments

Then what does count(get_object_vars($object)) do?
@CodeCaster it doesn't return the number of public variables in the object (other than running a count on the returned array). Just using count($object) will return the number as the answer above says.
get_object_vars() will return an array of those properties, but you then need to do a count() on that array to get the number of properties, so not directly accessible
The question was not whether it could be done with one and only one function call.
0

What is the name of the integer between brackets in a var_dump of an object. And how do I acces it with PHP?

I'm referring to the (3) in the next example.

That's the number of public members it has (namely, ID, Name and Date). If you want to know that number, you could just use count( get_object_vars( $object ) ):

<?php

$foo = new stdClass;
$foo->foo = 42;
$foo->bar = 42;
$foo->baz = 42;

var_dump( count( get_object_vars( $foo ) ) );

2 Comments

if the object has private properties, var_dump will count those while get_object_vars doesn't - so this doesn't always give the expected result.
@oezi It will when you're counting public properties though, and SimpleXMLElement happens to have only public properties, just like stdClass.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.