-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Allow to set undefined properties in yii\base\Object #12021
Comments
The behavior of being strict about setting undefined properties could be moved into a trait same as ability to access getters as properties. |
Not allowing to set unexisting properties is a feature I would keep tied to the configuration feature of yii as it helps prevent typos in configuration files. We could split the features into traits so one can implement what is needed dependent of the purpose of the class. |
Agreed with @cebe |
For 2.1 I'd like to propose a change in
yii\base\Object
to make it behave like standard PHP objects again, when it comes to setting undefined properties.This change could also be configurable, if some consider it to be too drastical.
What is it about?
You will see an
yii\base\UnknownPropertyException
.How does PHP behave usually in this case?
Any class in PHP allows to set undefined properties, just like
stdClass
. So if you take the above example but don't extend fromyii\base\Object
, it will work:All Yii2 objects break this standard behavior, so you lose a feature that works for all other PHP classes.
Why does this matter?
Say you have a complex SOAP service with 100s of SOAP types, some nested into each other. For example there could be a
User
type object somewhere in the SOAP result. By default PHP will return a instance ofstdClass
with all the properties set. Something like this:Now you wish to have a
fullName
property. You therefore could define a custom class with a getter method like:And configure it in the
classmap
option of yourSoapClient
:This works, but you still have to call
$user->getFullName()
. If you like to do it the Yii way and have access to$user->firstName
you can't, because your custom class can not extend fromyii\base\Object
. You would have to define each and every property of theUser
SOAP type manually - something you don't have to do in pure PHP classes or instdClass
in this situation.Proposed solution for Yii 2.1
Instead of throwing an error in
Object::__set()
for undefined properties, it should simply set the property:I'm not quite sure what would be a good way to make this optional.
Object
should stay free of any properties as it's a base class. And defining another method like__allowUndefined()
seems like overkill. Maybe a property likeprotected $__allowSetUndefined = false;
?The text was updated successfully, but these errors were encountered: