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