Comments (8)
@berosoboy commented on Mar 7, 2018, 2:13 PM UTC:
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.
from yii-core.
@samdark commented on Mar 7, 2018, 6:27 PM UTC:
OK. Let's have another look at it. Yii indeed enhances default PHP objects behavior adding several features:
- Getters/setters.
- Strict errors when non-existing property is set.
On top if it, Component adds:
- Events.
- Behaviors.
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 __get()
, __set()
and other magic methods are used by all these features therefore I don't immediately see a good way to achieve that.
from yii-core.
@samdark commented on Mar 7, 2018, 8:59 PM UTC:
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.
from yii-core.
@mikehaertl commented on Mar 8, 2018, 7:28 AM UTC:
I don't know a perfect way either. Each thinkable solution has shortcomings. Still, my current favourite is having a (optional!!) class property like:
public $__strictGetSet = false;
Only if defined this would disable checks for existing properties. The default would be the same behavior we have now.
from yii-core.
@mikehaertl commented on Mar 8, 2018, 7:34 AM UTC:
You can create your custom Object class that extends yii\base\Object and overrides the __set with your proposed code:
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.
from yii-core.
@SilverFire commented on Mar 8, 2018, 9:07 AM UTC:
I'd rather see it as LooseGetSetInterface
that may be applied to class to indicate that __get()
/__set()
methods should not check property existence.
from yii-core.
@mikehaertl commented on Mar 8, 2018, 9:54 AM UTC:
SilverFire You mean an empty interface (interface LooseGetSetInterface {}
) that's only used to flag certain classes? I like that idea. It's also straightforward to check for that in the magic method.
from yii-core.
There will be no yii\base\Object in Yii 3.0.
from yii-core.
Related Issues (20)
- `data` dirs in tests HOT 5
- Debug code in Model? HOT 3
- \yii\base\Application constructor parameter HOT 2
- 我认为yii3最应该解决的就是composer包过大的问题 HOT 2
- Should not require yii-web HOT 11
- Enhancement: BaseJson::decode supports decode options HOT 1
- Discuss: Do we still need controllers? HOT 2
- Enhancement: StringHelper::isEmpty() HOT 7
- can't visit yii forum site HOT 1
- Review helpers naming and usage HOT 33
- Url::ensureScheme should fail hard for non absolute paths HOT 2
- What is the current dependency between repo? HOT 20
- Yii3 assets path not correctly reference if installed in subfolder HOT 2
- SOLID compliance HOT 3
- Custom reference syntax to class or class member in comments make impossible to navigate in IDE HOT 19
- Change namespace to Yiisoft\Core? HOT 8
- When will yii3 be released? HOT 1
- Request to separate FileHelper from yii-core HOT 19
- Pack requirements checker into separate package HOT 1
- Repo merges into a repo and then splits HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from yii-core.