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 Answers2

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;
    }
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
0
<?php

require_once "../Controllers/UserController.php";

class Controller
{
    public function __construct()
    {
    }
    public function call(){
        // echo 1;
        $a = new UserController();
        $a->hi();
    }
}
John V
  • 875
  • 6
  • 12
  • 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. – FluffyKitten Aug 20 '20 at 08:36