Created
March 20, 2019 10:19
-
-
Save kobus1998/88a07c26b3cbf584fb7ae30ddddb3106 to your computer and use it in GitHub Desktop.
pattern edit class inside class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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