On Sun, Sep 9, 2012 at 3:51 AM, Patrick Schaaf <p...@bof.de> wrote:
> Hi,
>
> I'm curious whether the ORDER of the class properties returned by
> get_class_vars(), is in some way guaranteed. I do not find mention of it
> in the documentation.
>

The only real guarantee is that of how the class properties are stored
in the hashtable. get_class_vars() is essentially just reading from a
hashtable of class properties.

> I'm especially interested in the order of static class variables
> returned.
>
> Testing, using PHP 5.4.6, I find that there seems to be a stable order,
> which suits my reasons for looking into this quite nicely. Here is what
> I find:
>
> - static properties defined directly in the class itself come first, in
> definition (source) order
> - static properties defined directly in parent classes come next, in
> definition order of their respective sources, and going up the class
> tree. So grandparent properties come after parent properties
> - static properties contributed by traits, all come AFTER the properties
>   from the class source itself, including all ancestors
> - static properties contributed by a single trait come in definition
> (source) order of that trait definition
> - with static properties from several traits, those of the HIGHEST
>   class in the inheritance tree some first, and those from the class
>   under inspection come last. This is exactly the opposite order of
>   what happens with properties from the classes themselves.
>
> As it happens, the ordering I find for static properties contributed by
> traits, is exactly what I was looking for. But is it guaranteed, or
> could such a guarantee be made?
>

The order they're stored in the hashtable is as you've pointed out
dependent on loading the definitions of the classes in the case of
class variables. However, it's not a guarantee that get_class_vars()
makes since it doesn't guarantee any order. It just so happens that it
also doesn't try to intercede in whatever order the class variables
are stored in in the hashtable.

It's not like depending on the order of class variable definitions is
something that crops up in any practical use cases that I've
encountered. I wouldn't depend on order too heavily here.

Anything were order is important to you should use an Array since
that's what a PHP Array type guarantees (i.e. order).

> best regards
>   Patrick
>
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to