Gp Master
Gp Master

Reputation: 456

Configuration object Pattern in PHP

I am trying to understand what Configuration Object pattern is. For instance in php, how to write the following class using Configuration Object pattern?

class User extends People {
    public function __construct($name , $email){}
}

Upvotes: 1

Views: 2354

Answers (1)

Nikita Leshchev
Nikita Leshchev

Reputation: 1844

Like this:

class User extends People {
    private $name;
    private $email;

    public function __construct($conf){
        $this->name = $conf->name;
        $this->email = $conf->email;
    }
}

Of course this pattern is for much more complicated situations, when you want to configure DB connection or some API client or smth like that. This pattern is helpful, when you need pass many properties or do some actions with them.

I think here's good article about this pattern: https://code.tutsplus.com/tutorials/whats-a-configuration-object-and-why-bother-using-it--active-11580

Upvotes: 2

Related Questions