- substr: documentation ( source)
- get_class_methods: documentation ( source)
<?php
trait OverloadConstructor {
public function __construct(... $args) {
$length = strlen('initMethod');
foreach (get_class_methods($this) as $method) {
if (substr($method, 0, $length) === 'initMethod') {
$this->{$method}(... $args);
}
}
}
}
trait Init1 {
protected function initMethodInit1(... $args) {
echo 'initMethodInit1' . PHP_EOL;
}
}
trait Init2 {
protected function initMethodInit2(... $args) {
echo 'initMethodInit2' . PHP_EOL;
}
}
class InitTest {
use OverloadConstructor, Init1;
protected function initMethodInitTest(... $args) {
echo 'initMethodInitTest' . PHP_EOL;
}
}
class ChangeBehaviourOfInitTest extends InitTest {
use Init2;
protected function initMethodChangeBehaviourOfInitTest(... $args) {
echo 'initMethodChangeBehaviourOfInitTest' . PHP_EOL;
}
}
new InitTest();
new ChangeBehaviourOfInitTest();