Giter Site home page Giter Site logo

Comments (8)

 avatar commented on August 30, 2024

@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.

 avatar commented on August 30, 2024

@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.

 avatar commented on August 30, 2024

@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.

 avatar commented on August 30, 2024

@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.

 avatar commented on August 30, 2024

@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.

 avatar commented on August 30, 2024

@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.

 avatar commented on August 30, 2024

@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.

samdark avatar samdark commented on August 30, 2024

There will be no yii\base\Object in Yii 3.0.

from yii-core.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.