-
-
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 #15845
Comments
You can create your custom Object class that extends yii\base\Object and overrides the __set with your proposed code: public function __set($name, $value)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter($value);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
} else {
// Instead of throwing exception:
$this->$name = $value;
}
} And your custom classes can extends your custom Object. |
OK. Let's have another look at it. Yii indeed enhances default PHP objects behavior adding several features:
On top if it, Component adds:
Ideally we want to divide these into traits and be able to use any combination of these traits for any object. The problem is that same |
We could probably add a specialized trait that could be configured with an array of handlers for the purpose but I do not see any way for it to be as performant as current solution. |
I don't know a perfect way either. Each thinkable solution has shortcomings. Still, my current favourite is having a (optional!!) class property like:
Only if defined this would disable checks for existing properties. The default would be the same behavior we have now. |
@berosoboy Sure, but I may also want to extend a more complex Yii class and still disable that behavior. With your suggestion I'd basically end up duplicating a lot of Yii classes in that case. |
I'd rather see it as |
@SilverFire You mean an empty interface ( |
This issue was moved by samdark to yiisoft/yii-core#41. |
I have to come back to my closed issue #12021. It seems that this is still not fixed in Yii 2.1.
My description and use case there is still valid.
The outcome of #12021 was, that this should be implemented via a trait. But it seems this never happened in 2.1.
tl;dr: I want to use the magic getter/setter methods from
yii\base\BaseObject
to be available in my custom classes. But I don't want that this changes PHP's default behavior, where you can also set undefined properties on a class.Yii changes this default behavior of PHP:
The text was updated successfully, but these errors were encountered: