fork download
  1. <?php
  2. ini_set('display_errors', 1);
  3.  
  4. class ParentClass
  5. {
  6. public function __construct()
  7. {
  8. }
  9.  
  10. public function testMethod()
  11. {
  12. echo "this should work in both classes ";
  13. echo '<br>';
  14. }
  15. }
  16.  
  17. class ChildClass extends ParentClass
  18. {
  19. public function __construct(string $param1 = 'test', string $param2)
  20. {
  21. }
  22. }
  23.  
  24. $classes = ['ParentClass', 'ChildClass', 'ParentClass', 'ChildClass'];
  25.  
  26. foreach ($classes as $class) {
  27. $object = new $class();
  28. $object->testMethod();
  29. }
Runtime error #stdin #stdout #stderr 0s 82880KB
stdin
Standard input is empty
stdout
this should work in both classes <br>
Fatal error: Uncaught ArgumentCountError: Too few arguments to function ChildClass::__construct(), 0 passed in /home/lpoSIT/prog.php on line 28 and exactly 2 expected in /home/lpoSIT/prog.php:20
Stack trace:
#0 /home/lpoSIT/prog.php(28): ChildClass->__construct()
#1 {main}
  thrown in /home/lpoSIT/prog.php on line 20
stderr
PHP Fatal error:  Uncaught ArgumentCountError: Too few arguments to function ChildClass::__construct(), 0 passed in /home/lpoSIT/prog.php on line 28 and exactly 2 expected in /home/lpoSIT/prog.php:20
Stack trace:
#0 /home/lpoSIT/prog.php(28): ChildClass->__construct()
#1 {main}
  thrown in /home/lpoSIT/prog.php on line 20