Skip to content

Object to Array conversion tests - #358

Closed
Ocramius wants to merge 2 commits into
php:masterfrom
Ocramius:test/object-to-array-keys
Closed

Ocramius wants to merge 2 commits into
php:masterfrom
Ocramius:test/object-to-array-keys

Conversation

@Ocramius

Copy link
Copy Markdown
Contributor

This PR simply introduces two tests to prevent unwanted BC breaks in how objects are converted to arrays.

This (as far as I know) happens in two cases:

  • When explicitly calling $data = (array) $object;
  • When using __sleep

This PR simply adds tests that verify the keys of the arrays being generated.

@Ocramius Ocramius mentioned this pull request Jun 20, 2013
@nikic

nikic commented Jun 20, 2013

Copy link
Copy Markdown
Member

This seems more like a behavior we should fix rather than test. (array) $object should return the public properties only (similarly to what happens when iterating over the object with foreach).

@Ocramius

Copy link
Copy Markdown
Contributor Author

@nikic I'm actually gaining huge performance improvements from this by avoiding reflection. If this is unexpected behavior, then an alternate solution that gives me an array copy of an object could be required.

This representation actually makes a lot of sense to me, since it allows me to get data for larger hierarchies too:

class Foo
{
    private $bar;
}

class Bar extends Foo
{
    private $bar;
}

class Baz extends Bar
{
    private $bar;
}

var_dump((array) new Baz()); // gives me 3 keys - awesome!

Via reflection, I have to re-build all this myself, resulting in quite a mess, especially given the ORM performance requirements. I actually think it's a really nice feature.

@Ocramius

Copy link
Copy Markdown
Contributor Author

Recap after discussion on http://chat.stackoverflow.com/transcript/message/10116460#10116460 :

  • This PR is not about "freezing" the feature
  • This PR aims at providing documentation for the feature, so that eventual BC breaks explicitly show up in a committers' build
  • I have no problems with the API changing in future, but I'd gladly see a bulk "write-" and "read-" non-public fields API that allows me to break OO rules as I'm doing here (for a good reason) without using reflection (because of the performance implications)

/cc @jpauli

@smalyshev

Copy link
Copy Markdown
Contributor

I'm not a big fan of exposing internal implementation, but I guess there's no harm in making any further change in this area explicit.

@php-pulls

Copy link
Copy Markdown

Comment on behalf of stas at php.net:

merged

@php-pulls php-pulls closed this Jun 23, 2013
@Ocramius
Ocramius deleted the test/object-to-array-keys branch June 23, 2013 23:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants