Welcome Guest, Not a member yet? Register   Sign In
Custom Result Objects & Setter
#1
Shocked 

Hi, I started to use CI4 three days ago and today I'm facing a problem with "Custom Result Objects". Here is the class of my custom object :

PHP Code:
class Transaction
{
    protected int $id;
    protected string $name;
    protected float $amount;

    public function __get($key)
    {
        if (property_exists($this$key)) {
            return $this->{$key};
        }
    }

    public function __set($key$value)
    {
        if (property_exists($this$key)) {
            $this->{$key} = $value;
        }
    }


The problem is when I'm using the function "getCustomResultObject" the "__set" method is never called, attributes are directly updated. Indeed, I tried to modify the setter to update my attributes using the $key & $value variables to do some operations, but the setter is never called. That is strange because I saw in the documentation :

Quote:The object will have all values returned from the database set as properties. If these have been declared and are non-public then you should provide a __set() method to allow them to be set.

And indeed if I remove the "__set" method, I get a error message "Cannot access protected property App\Entities\Transaction::$name". So the __set method is necessary to update the "protected" attributes, but the method is never used. So is it a bug, or I missed something ?

Thanks for your help.
Reply


Messages In This Thread
Custom Result Objects & Setter - by Fabien72 - 11-03-2023, 12:49 PM
RE: Custom Result Objects & Setter - by kenjis - 11-03-2023, 04:28 PM
RE: Custom Result Objects & Setter - by ozornick - 11-03-2023, 10:26 PM
RE: Custom Result Objects & Setter - by kenjis - 11-03-2023, 10:41 PM
RE: Custom Result Objects & Setter - by ozornick - 11-04-2023, 02:24 AM
RE: Custom Result Objects & Setter - by Fabien72 - 11-05-2023, 09:56 AM



Theme © iAndrew 2016 - Forum software by © MyBB