0

I'm new to PHP, I'm trying to require UserController.php from Controller.php but all I get is "HTTP ERROR 500" in browser. What's going on here?

Controller.php

class Controller { public function __construct() { } public function call(){ // echo 1; require_once "../Controllers/UserController.php"; } } 

UserController.php

class UserController { public function __construct() { echo '111111111'; } public function hi(){ echo '1'; } } $a = new UserController(); $a->hi(); 
2
  • 1
    You can't define a class inside a function definition. Commented Aug 20, 2020 at 5:54
  • You should have the require_once line outside the class Controller. Commented Aug 20, 2020 at 5:55

2 Answers 2

1

Class definitions can't be nested inside functions or other classes. So you shouldn't have that require_once line inside a function definition. Move it outside the class.

require_once "../Controllers/UserController.php"; class Controller { public function __construct() { } public function call(){ // echo 1; } } 
Sign up to request clarification or add additional context in comments.

Comments

0
<?php require_once "../Controllers/UserController.php"; class Controller { public function __construct() { } public function call(){ // echo 1; $a = new UserController(); $a->hi(); } } 

1 Comment

Code-only answers are discouraged on Stack Overflow because they don't explain how it solves the problem. Please edit your answer to explain what this code does and how it answers the question, so that it is useful to the OP as well as other users with similar issues.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.