php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40477 object's read property handler not used in zend_print_zval_r_ex
Submitted: 2007-02-14 09:56 UTC Modified: 2007-02-15 18:50 UTC
From: piotrek dot pokora at gmail dot com Assigned: helly (profile)
Status: Not a bug Package: Variables related
PHP Version: 5.2.* OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: piotrek dot pokora at gmail dot com
New email:
PHP Version: OS:

 

 [2007-02-14 09:56 UTC] piotrek dot pokora at gmail dot com
Description:
------------
zend_print_zval_r_ex uses object's get_properties handler and prints properties HashTable without using read_property handler.

For custom object handlers hooks it means that get_properties should be also implemented as hook which leads to too much overloaded implementation as get_properties seems to be invoked for read and write hooks.

Taking performance into account , one would sacrifice performance problem for print_r ( which is not used in real production environment ) which could use object's read_property handler hook. 

Reproduce code:
---------------
Implement read and write property hooks for object.
Assuming that property's data is set in C extension one is able to call:

echo $object->prop; 

which prints property's value, while 

print_r($object);

prints object's properties without values. 

I know I can implement own get_properties hook , but unfortunatelly I can not find suitable docs for such implementation.

I think one should be able to reproduce it with mysqli extension which implement only get and read property hooks.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-02-14 11:03 UTC] tony2001@php.net
Of course get_properties() is required, how else do you think it should work? You need to know the name of the property to be able to call read_property().
The fact that you can't find any docs for get_properties()(which is implemented quite a number of times in PHP sources) is hardly a PHP bug.
Please discuss custom extension writing in pecl-dev@lists.php.net mailing list.
 [2007-02-14 14:49 UTC] piotrek dot pokora at gmail dot com
>Of course get_properties() is required, how else do you >think it should work?

As it works without get_properties.
It's enough to add own read and write property hook and some function which "registers" properties of an object in constructor.  

It works _perfect_ for reading property, assigning value to it. And the same for derived classes.

Look at mysqli implementation.
It DOES NOT contain get_properties hook and works. Why?

>You need to know the name of the property to be able to call
>read_property().

Hook function requires zval *member as second argument.
I do not know internals , please tell me :)

read_property hook works _perfect_ without get_properties hook.

>The fact that you can't find any docs for >get_properties()(which is
>implemented quite a number of times in PHP sources) is >hardly a PHP bug.

Yes, but the logic bug here is the fact that always internal zend functions "operate" in object's handlers with this little exception of print_t or var_dump when custom hash table is taken into account.

So , to sum up. I can not find any not working part of code with only read and write property hook , except this one for print_r and var_dump.

>Please discuss custom extension writing in >pecl-dev@lists.php.net mailing list.

Will try :)
 [2007-02-14 15:00 UTC] tony2001@php.net
>As it works without get_properties.
Oh, yes it does work, but you can't get the list of properties without implementing get_properties().

>It's enough to add own read and write property hook
No, it's obviously not enough. See below why.

>Look at mysqli implementation.
>It DOES NOT contain get_properties hook and works. Why?

var_dump($mysqli_object);
Do you see anything? No. 
That's why.
 [2007-02-14 15:31 UTC] piotrek dot pokora at gmail dot com
>>Look at mysqli implementation.
>>It DOES NOT contain get_properties hook and works. Why?

>var_dump($mysqli_object);
>Do you see anything? No. 
>That's why.

Is it difficult to add posibility to read object's property from its own read_property hook?  This bug is just about this.

The point here is: even I add own get_properties function I can not update them in object's hash properties scope. 
At least it is not done via standard write_property handler.
So I need to use own or standard hooks + additional object's properties hash table update which duplicates operation.

Of course , get_properties hook can be missed in favour of own properties registration.

But coming back to mysqli example:

part of object constructor:

 zend_object_std_init(&intern->zo, class_type TSRMLS_CC);     zend_hash_copy(intern->zo.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref,
                    (void *) &tmp, sizeof(zval *));

The same hash is used by print_r, and doesn't have to be used in read or write property hook to make those hooks work.

And coming back to this bug summary:
All I report here is the issue of print_r and var_dump which uses zval.value.obj.properties hash table which doesn't have to be used in read or write property hooks. 

My hook doesn't require hash table value updates, all I need is object's property name ( hash key in this case ).

print_r and var_dump uses hash table ( only ) so I need to update keys' values to make these function work.

Why I defined my own hooks? Because print_r or var_dump or any other OOP featire in ZEND&PHP should respect my hooks.
Right? 

If ZEND requires object's properties hash table value updates then I do not need read property hook as I can get property value from hash table.

If ZEND requires updated hash table ( with keys and values ) in function like print_r , how can I avoid setting the same value twice? Once in write hook and once as hash value?

I can add own get_properties hook. But print_r and var_dump will print values of properties only ( and only ) when I update hash table which is not mandatory in read or write property hook.
 [2007-02-14 15:33 UTC] tony2001@php.net
Please use pecl-dev.
 [2007-02-14 15:51 UTC] piotrek dot pokora at gmail dot com
>Please use pecl-dev.

Please, make this bug open for the sake of logic and consistency.

I know how to write own get_properties hook.
I know how to workaround this problem ( yes it's workaround now, ugly though ).
I know what any php extension developer is able to tell me.

It doesn't change the fact that print_r or var_dump doesn't respect object's read_property hook.

It must be clear and clean that (pseudocode):
 if(zval.type == IS_OBJECT) 
    zval.value.obj.handlers.read_property

Will pecl-dev advice will fix this issue? No.
Is there another zend bug tracker and pecl-dev will help me find it?

Or should I prepare patch and send somewhere?
 [2007-02-14 15:56 UTC] tony2001@php.net
Again, please use pecl-dev, this is not a forum.
 [2007-02-14 16:08 UTC] piotrek dot pokora at gmail dot com
> Again, please use pecl-dev, this is not a forum.

http://pecl.php.net/bugs/ points directly to bugs.php.net.
So I think I am at correct bugtracker.
 [2007-02-14 16:10 UTC] piotrek dot pokora at gmail dot com
Sorry, changed summary by accident.
 [2007-02-14 16:38 UTC] tony2001@php.net
There is not bug and I asked you to discuss issues rising in custom PHP extensions in pecl-dev@lists.php.net.
Please subscribe to this list and lets discuss it there.

 [2007-02-14 17:06 UTC] piotrek dot pokora at gmail dot com
> There is not bug 

A feature?

>and I asked you to discuss issues rising in custom PHP
extensions in pecl-dev@lists.php.net.

What should I ask for at pecl-dev@lists.php.net?
Why function which prints object's properties doesn't use its read_property hook handler? 

Again , for clarification.
What is read_property handler hook for?

I think you should :

Close this bug and I will open another one.

or

Clarify why it's not a bug.

For me it's obvious that it's a bug when implementation uses user's hook _almost_ always instead of always.

Or object is no longer an object when passed to print_r or var_dump?

Do you want to tell me that Zend provides possibility to register own hooks but it's OK when sometimes it's not used for user defined code?
 [2007-02-14 17:20 UTC] tony2001@php.net
>A feature?

A total misunderstanding on your side.

>Why function which prints object's properties doesn't use
>its read_property hook handler? 

Yes. Just re-read your question.
"Why a function which prints ALL the properties does not use read_property()?"
read_property() requires "property_name" argument.
Care to imagine where it should be taken from? Out of your mind?
That's why get_properties() exist - it returns the hashtable with ALL object properties.

>Again , for clarification.
>What is read_property handler hook for?

To read a property specifed by the GIVEN NAME.

>For me it's obvious that it's a bug when implementation 
>uses user's hook _almost_ always instead of always.

For me it's obvious that you misunderstand the whole concept.

Please STOP using this bug tracking system as a forum for your questions, use the mailing list instead.
 [2007-02-14 20:51 UTC] piotrek dot pokora at gmail dot com
OK, let's talk code.

Sources.
GObject and GValue wrappers for a case we talk about:
http://midgard.tigris.org/source/browse/midgard/src/apis/php4/php_midgard_gobject_generic.c?rev=1.5&view=log

midgard_config class constructor and methods:
http://midgard.tigris.org/source/browse/midgard/src/apis/php4/php_midgard_config.c?rev=1.4&view=markup

PAY SPECIAL ATTENTION! There's no get_properties hook function.

PHP code:

$a = new midgard_config();
/* $a has underlying GObject object so properties for this object are already assigned */
$a->read_file("my_config", TRUE);
print_r($a);
echo $a->dbtype;

Output:

midgard_config Object
(
    [__res] => Resource id #8
    [dbtype] =>
    [database] =>
    [dbuser] =>
    [dbpass] =>
    [host] =>
    [blobdir] =>
    [logfilename] =>
    [schema] =>
    [defaultlang] =>
    [loglevel] =>
    [tablecreate] =>
    [tableupdate] =>
    [testunit] =>
    [midgardusername] =>
    [midgardpassword] =>
    [authtype] => 0
    [pamfile] =>
)
MySQL

Do you see 'MySQL' value in print_r output?
Why then echo $a->dbtype; outputs MySQL? Even if I use standard ( not defined by me ) get_properties handler??

Because language respect my read_property hook, isn't it.
 
Do you want similiar code for derived class?
Or maybe you can look at zend_print_zval_r_ex and notice that print_hash is used for IS_ARRAY and IS_OBJECT value type?

DO YOU SEE read_property hook invoked by print_hash function??

Maybe I misunderstood something here but this is not the fact that IS_ARRAY != IS_OBJECT and zend_print_zval_r_ex 
ignores completely user's read_property hook.
 [2007-02-15 09:03 UTC] tony2001@php.net
pecl-dev@lists.php.net
 [2007-02-15 09:32 UTC] piotrek dot pokora at gmail dot com
> pecl-dev@lists.php.net

Is this a list for reporting ZEND bugs?

http://cvs.php.net/viewvc.cgi/ZendEngine2/zend.c?revision=1.393&view=markup

function: static void print_hash(HashTable *ht, int indent, zend_bool is_object TSRMLS_DC)

printing value:
ZEND_PUTS("] => ");
zend_print_zval_r(*tmp, indent+PRINT_ZVAL_INDENT TSRMLS_CC);
ZEND_PUTS("\n");

`tmp` pointer is not a pointer for a zval returned by ANY read_property. Neither standard nor user defined one.

If you are going to keep this bug with bogus status, I am going to submit another one.

Logic and consistency is broken here. And this is absolutely a bug. Behaviour of this function's part is unchanged since  ZEND1 engine.
 [2007-02-15 09:36 UTC] mike@php.net
Stop ranting and grasp the facts.
 [2007-02-15 09:48 UTC] tony2001@php.net
>> pecl-dev@lists.php.net
> Is this a list for reporting ZEND bugs?

No, this is a list when I'm ready to explain any question regarding Zend Engine sources to a person who haven't ever heard of respect.
 [2007-02-15 10:12 UTC] piotrek dot pokora at gmail dot com
>> Is this a list for reporting ZEND bugs?

>No, this is a list when I'm ready to explain any question regarding Zend
>Engine sources to a person who haven't ever heard of respect.

Please, forgive me. But don't you think that documentation privided by ZEND clearly states:

"read_property - returns zval *, containing the value of the
property. Is used when value of the property should be retrieved for reading."

It doesn't tell about any exceptions.

I will be more than glad to ask you about such exceptions at  pecl-dev list but unfortunatelly I didn't get any confirmation after using ( few times ) submit form at http://pecl.php.net/support.php#lists.

If I can not subscribe to this list with this form , please subscribe me using my mail. 

I am looking forward for detailed clarification.
 [2007-02-15 18:44 UTC] helly@php.net
PHP 5.3 will and PHP 6 has already a handler to solve the issue. As long as we are speaking of internal stuff that means the only thing we have to do is check whether the mentioned helper function uses that new handler in PHP 6. Care to do so? 
 [2007-02-15 18:45 UTC] helly@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.

The above closes the bug
 [2007-02-15 18:50 UTC] tony2001@php.net
Markus, read it from the beginning please.
And see pecl-dev also.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 11:01:30 2024 UTC