Re: print_r unaware of object's read_property handler

From: Date: Thu, 15 Feb 2007 12:15:11 +0000
Subject: Re: print_r unaware of object's read_property handler
References: 1 2 3  Groups: php.pecl.dev 
Request: Send a blank email to pecl-dev+get-4516@lists.php.net to get a copy of this message
On 02/15/2007 02:48 PM, Piotr Pokora wrote:
antony@zend.com (Antony Dovgal) wrote:
In order to use read_property() you need to know the name of the property.
Yes, I know and standard get_properties is perfectly fine wih this.
print_r() and var_dump() output all properties of the object, they do not know their names, they have only the object itself.
That's why I ask. Those functions do not follow object's handler but instead use only object's properties hash. Yes I know it's returned by get_properties handler , but read_property is implemented for reading property's value so this handler should be used to get value instead of hash table value.
Ok, show me in a pseudo-code how are you going to clap with only one hand, i.e. to use read_property() without knowing the property name. This is how it works atm: print_r(object) { properties = object->get_properties(); while(properties) { print *properties; properties++; } } You insist on implementing it like this: print_r(object) { property = object->read_property(<PROPERTY_NAME_TAKEN_DIRECTLY_FROM_YOUR_BRAIN>); print property; }
Therefor you need to implement get_properties() hook to return a hash table with all object properties.
That's __very__ clear. If I have possibility to add get_properties hook and read_property hook I should be able to define own handler which returns properties names from get_properties and read every property's value using read_property hook as I already know property name. Am I wrong here?
Yes. If you need to get only names of the properties, you can use keys of the hash returned by get_properties(). But returning only keys without the values does not make any sense, since it would require implementing and calling BOTH get_properties() AND read_property(), and the latter would be called for each property, adding a slowdown.
What I understand from your clarification is: get_properties returns object's properties read_property reads property knowing it's name Do I understand it correct?
Yes, that's correct.
If yes, why print_r doesn't read property value using read_property hook, even if I add own get_properties hook which returns all properties names?
Because get_properties() returns both names and values of the properties, we don't need to call read_property(), we already have the value in the has returned from get_properties().
Yes, of course I can implement get_properties hook that way so it can updates object's properties hash. But my question is: do I need such functionality if my read_property ( and write ) hook doesn't need object's properties hash?
Don't use it if you don't need it.
Or better , do I need to duplicate every property's value in hash table because print_r doesn't use my read_property handler?
"Duplicate every property's value"? Have you ever heard of functions and code reuse?
A new question is: What write_property handler hook should do?
Let's not add even more misunderstanding here, no new questions until the current one is not solved.
Obvious answer is that such hook must update object's properties hash. But why, if the only one purpose is print_r and var_dump functions?
$object->property = "new value";
And I think the last question: When and why object's read_property hook is ignored?
I don't think you really expect me to go over the sources and explain every internal function to you, do you? -- Wbr, Antony Dovgal

« previous php.pecl.dev (#4516) next »