Created
November 26, 2017 09:57
-
-
Save pyaesone17/2046fce2e83f9a0c1e8809385a698302 to your computer and use it in GitHub Desktop.
Go lang struct composition implementation in PHP
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 A { | |
public $dependencies = []; | |
public function __construct(...$arguments) | |
{ | |
$this->dependencies = $arguments; | |
} | |
public function __call($name, $arguments){ | |
foreach ($this->dependencies as $key => $dependency) { | |
$methods = get_class_methods($dependency); | |
if (in_array($name, $methods)) { | |
return call_user_func_array([$dependency,$name], $arguments); | |
} | |
} | |
} | |
public function __set($name, $value){ | |
foreach ($this->dependencies as $key => $dependency) { | |
$vars = get_object_vars($dependency); | |
if (array_key_exists($name, $vars)) { | |
$dependency->$name = $value; | |
} | |
} | |
} | |
public function __get($name){ | |
foreach ($this->dependencies as $key => $dependency) { | |
$vars = get_object_vars($dependency); | |
if (array_key_exists($name, $vars)) { | |
return $dependency->$name; | |
} | |
} | |
} | |
} | |
class B { | |
public $b; | |
public function hello() | |
{ | |
return "Say Hello From B"; | |
} | |
} | |
$a = new A(new B); | |
$a->b = "Setting from A"; | |
echo $a->hello(); | |
echo $a->b; | |
die(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment