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

If you have "protected $name" properties declared, they are automatically filled with values. 
When there is no property, __set() is executed. BUT, you re-check the declaration of the property (they are already filled in). property_exists() is unnecessary.

PHP Code:
// Class
class Notify {
    protected $id$user_id$title$is_read;

    public function __set($name$value)
    {
        $this->{$name} = $value;

        return $this;
    }
}
// Select result
$notify $model->builder()->limit(1)->get()->getCustomResultObject(Notify::class);
// Output
array:[
  0 
=> App\Controllers\Notify {#199 ▼
    #id: "5"
    #user_id: "1"
    #title: "New Message"
    #is_read: "yes"
    +"body""New post created!"
    +"created_at""2023-11-01 20:18:43"
  }

Simple CI 4 project for beginners codeigniter-expenses ( topic )
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