Skip to content

Instantly share code, notes, and snippets.

@kobus1998
Created March 20, 2019 10:19
Show Gist options
  • Save kobus1998/88a07c26b3cbf584fb7ae30ddddb3106 to your computer and use it in GitHub Desktop.
Save kobus1998/88a07c26b3cbf584fb7ae30ddddb3106 to your computer and use it in GitHub Desktop.
pattern edit class inside class
<?php
class First
{
private $second;
public function __construct($second)
{
$this->second = $second;
}
public function edit(callable $fun)
{
$fun($this->second);
return $this;
}
public function show()
{
echo $this->second->getValue();
}
}
class Second
{
private $value;
public function __construct($value)
{
$this->value = $value;
}
public function edit($value)
{
$this->value = $value;
return $this;
}
public function getValue()
{
return $this->value;
}
}
$second = new Second(123);
$first = new First($second);
echo $second->getValue() . "\n";
echo $first->edit(function ($second) {
$second->edit(567);
})->show() . "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment