0

So recently I try to make php MVC, and then I do boostrapping method for load classes to /public/index.php , when i was done with the boostrapping,I do simple testing which I try to perform the __construct() method inside the App class in /app/core/App.php, but when I open /public/index.php in browser,nothing happend, and I try to echo something inside /public/index.php just same, nothing happend

But when i remove the require_once "../app/init.php";, the echo just work perfectly, i have no idea what happend :(, please show me what's wrong with my code and why it can happend


My goal :

  1. make sure that my boostrapping correct
  2. to perform echo in browser for checking that the boostrapping success

Expected result :

  1. echo a string from App's __construct() to browser

My code :

project's directory tree

  1. Inside /app/core/App.php
<?php

class App{
    public function __construct(){
        echo "ok!";
    }
}
  1. Inside /app/init.php
<?php

require_once 'core/App.php';
require_once 'core/Controllers.php';
  1. Inside /public/index.php
<?php

require_once '../app/init.php';

$app = new App;
echo "test";

Edit : Errors message

 Warning: require_once(./core/App.php): Failed to open stream: No such file or directory in /opt/lampp/htdocs/phpmvc/app/init.php on line 3

 Fatal error: Uncaught Error: Failed opening required './core/App.php' (include_path='.:/opt/lampp/lib/php') in /opt/lampp/htdocs/phpmvc/app/init.php:3 Stack trace: #0 /opt/lampp/htdocs/phpmvc/public/index.php(3): require_once() #1 {main} thrown in /opt/lampp/htdocs/phpmvc/app/init.php on line 3
  • 1
    Probably some `require_once` call fails, but you have error display turned off…? Consult your server logs… – deceze Nov 11 '21 at 10:38
  • While developing, you should enable and display error messages. – Daniel W. Nov 11 '21 at 10:38
  • @deceze thanks for the information, right know i have error message, but unfortunately I still confused with these errors, – maruf_ilyasa Nov 11 '21 at 11:19
  • 1
    Note that relative imports are relative to the current *working directory*, not the file they're in (more specifically to the `include_path` mentioned in the error, which includes `.`, the current working directory). You usually want to use absolute paths explicitly to avoid that: `require_once __DIR__ . '/../...'`. – deceze Nov 11 '21 at 11:20

0 Answers0