I have this example class
Class RealUrlConfig
{
private $domains = [];
public function addDomain($host, $root_id)
{
$this->domains[] = [
'host' => $host,
'rootpage_id' => $root_id,
];
return $this; // <-- I added this
}
public function removeDomain($host)
{
foreach ($this->domains as $key => $item) {
if ($item['host'] == $host) {
unset($this->domains[$key]);
}
}
}
public function getDomains()
{
return $this->domains;
}
/**
* TODO: I need this
*/
public function addAlias($alias)
{
$last_modify = array_pop($this->domains);
$last_modify['alias'] = $alias;
$this->domains[] = $last_modify;
return $this;
}
}
Now I'm trying to create an option to add aliases to the hosts. I could just provide the original host name and the aliases and add that to the array, but I'm trying to do this without the original host - as nested method, so that I can execute it like this:
$url_config = new RealUrlConfig;
$url_config->addDomain('example.com', 1);
$url_config->addDomain('example2.com', 2)->addAlias('www.example2.com');
I added the return $this to addDomain method, so that it returns the object, but I fail to understand, how do I know which array to modify, since I get the whole object.
I could, of course, just read the last added domain from the domains array and modify that, but I', not quite sure if that's the right way.