Skip to content

[PropertyAccess] add support for magic call #7263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2013

Conversation

jaugustin
Copy link
Contributor

Hi,

I add support for magic call with the PropertyAccess

the is basic implementation, if no getter, isser, or hasser or _get is found and there is __call then the PropertyAccess call the getter

the same for setter.

This functionality is disable by default

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? seems OK (failure/errors are the same on master)
Fixed tickets #4683, #6413, #5309
License MIT
Doc PR symfony/symfony-docs#2472
  • submit changes to the documentation

@bschussek is this ok ?

@@ -224,6 +253,9 @@ private function &readProperty(&$object, $property)
} elseif ($reflClass->hasMethod('__get')) {
// needed to support magic method __get
$result[self::VALUE] = $object->$property;
} elseif ($this->magicCall && $reflClass->hasMethod('__call')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should first check the public property before __call IMO to be more BC

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stof the problem with that is __call won't be called if you have a private property, because exception is raised.
This is disable by default so this is BC :)
maybe I can talk with @bschussek to refactor the way PropertyAccess throw exceptions without trying next case

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see #7711

@vicb
Copy link
Contributor

vicb commented Mar 5, 2013

No need for FQCN in phpdoc.
please use "Boolean" rather than bool
thanks

@jaugustin
Copy link
Contributor Author

@vicb fixed ;)

@jaugustin
Copy link
Contributor Author

I add the documentation for this PR

*
* @return PropertyAccessor
*/
public function enableMagicCall($enable = true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the argument $enable and add a method disableMagicCall() instead.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw. both enableMagicCall() and disableMagicCall() should be moved to a new class PropertyAccessorBuilder. PropertyAccessor is immutable. (see how the same pattern is implemented in the Validator component)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @bschussek

@jaugustin
Copy link
Contributor Author

@bschussek I created the PropertyAccessorBuilder, tell me if this is what you expected.

@@ -24,12 +24,15 @@ class PropertyAccessor implements PropertyAccessorInterface
const VALUE = 0;
const IS_REF = 1;

private $magicCall = false;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is redundant if you are defining it in the constructor

@webmozart
Copy link
Contributor

Looks good apart from the few mentioned issues! Please do also add this to the component's CHANGELOG.

@webmozart
Copy link
Contributor

And please also squash your commits when you're done.

@jaugustin
Copy link
Contributor Author

@bschussek naming fixed, commit squashed :)

@fabpot
Copy link
Member

fabpot commented Apr 21, 2013

I don't understand why we would need PropertyAccessorBuilderInterface. Is it really needed?

@jaugustin
Copy link
Contributor Author

@fabpot I create the builder like ValidatorBuilder was created and it has an interface so I do the same.

@bschussek what is your point of view on this ?

If it's not needed I can remove it :)

@fabpot
Copy link
Member

fabpot commented Apr 21, 2013

Frankly, I don't understand why we have a ValidatorBuilderInterface either. It does not make sense to me.

@webmozart
Copy link
Contributor

@fabpot You don't understand why we have the interface or why we have the builder?

We have the interface so we can type-hint against the builder without type-hinting against the implementation, nothing more.

We have the builder so that users can configure Validator/PropertyAccessor objects more easily, without depending on the exact argument order and default values of their constructors (see Builder Pattern). So basically it's the OO equivalent of

$validator = new Validator(array(
    // configuration
));

only a bit more convenient because your IDE tells you how to configure the builder, while it doesn't tell you which entries you can pass to the array.

@webmozart
Copy link
Contributor

Also, configuration code can be extracted into the builder, as done in ValidatorBuilder for example.

@webmozart
Copy link
Contributor

👍 on this PR

@webmozart
Copy link
Contributor

Please add PropertyAccess::getPropertyAccessorBuilder() too.

@fabpot
Copy link
Member

fabpot commented Apr 21, 2013

"We have the interface so we can type-hint against the builder without type-hinting against the implementation, nothing more." <- that part does not make sense to me. Why not type-hinting on the implementation for a builder? You will never have any other implementation anyway.

@webmozart
Copy link
Contributor

You will never have any other implementation anyway.

I thought that so many times in other cases, until it happened :P Just to make sure.

add a new propertyAccessorBuilder to enable / disable the use of __call by
the PropertyAccessor
@jaugustin
Copy link
Contributor Author

@bschussek done

@fabpot
Copy link
Member

fabpot commented Apr 21, 2013

I thought that so many times in other cases, until it happened :P Just to make sure.

Come on. Have a look at the builder, I can't buy that you will have another implementation. And anyway, even if you can come up with something, you can still extend the existing one. But having such a specialized interface for a builder seems overkill to me.

@webmozart
Copy link
Contributor

Have a look at the builder, I can't buy that you will have another implementation.

The builder is very simple as of now because important functionalities are still missing, such as the generation and caching of class-specific property accessors, configurable property paths etc. With these things, the builder code will become more complicated as well.

I don't think that we in the core will have another implementation of the interface, but userland projects might well do if they want to add some functionality. I don't see why they should have to extend a class here when we provide interfaces everywhere else. After all, creating an interface doesn't cost us anything.

@webmozart
Copy link
Contributor

I talked with @fabpot again and both of our sides have valid arguments. Apart from this specific case, it's a general decision for our API design, also in other places of the framework. I created a poll to collect feedback from the community.

@stof
Copy link
Member

stof commented Apr 23, 2013

The issue with the interface for the builder is that the BC concern will prevent us adding anything new in the builder as it would be a BC break for other implementations.
Creating an interface costs something as soon as we look at BC.
IMO, the builder does not need an interface.
Thus, will you really inject the builder in your objects ? I think you should inject a PropertyAccessorInterface and use the builder outside your object to configure it

@ghost
Copy link

ghost commented Apr 23, 2013

@stof has nailed the argument very well for this particular case.

@webmozart
Copy link
Contributor

The poll ends tomorrow night (April 24 23:55 CET). I'll post the results then.

fabpot added a commit that referenced this pull request Apr 25, 2013
This PR was merged into the master branch.

Discussion
----------

[PropertyAccess] add support for magic call

Hi,

I add support for magic call with the `PropertyAccess`

the is basic implementation, if no `getter`, `isser`, or `hasser` or `_get` is found and there is `__call` then the PropertyAccess call the getter

the same for setter.

This functionality is disable by default

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | seems OK (failure/errors are the same on master)
| Fixed tickets | #4683, #6413, #5309
| License       | MIT
| Doc PR        | symfony/symfony-docs#2472

- [x] submit changes to the documentation

@bschussek is this ok ?

Commits
-------

a785baa [PropertyAccess] add support for magic call, related to #4683
fabpot added a commit that referenced this pull request Apr 25, 2013
@fabpot fabpot merged commit a785baa into symfony:master Apr 25, 2013
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.

7 participants