0
votes

I haven't seen any error in my files but when I run my code it shows me the following error:

Warning: require_once(Core.php): failed to open stream: No such file or directory in C:\xampp\htdocs\completed\inc\autoload.php on line 7

Fatal error: require_once(): Failed opening required 'Core.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\completed\inc\autoload.php on line 7

My code is:

classes/Core.php

 <?php
  class Core {

   public function run() {
      ob_start();
      require_once(Url::getPage());
      ob_get_flush();
     }

}

inc/autoload.php

 <?php
 require_once('config.php');

function __autoload($class_name) {
   $class = explode("_", $class_name);
   $path = implode("/", $class).".php";
   require_once($path);
}

index.php

<?php
require_once('inc/autoload.php');
$core = new Core();
$core->run();
4
the classes exist under the classes folder, but you are trying to include from the current directory. - Jonathan Kuhn
i created a config.php file under inc folder where i use set_include_path() function and i call it first in the autoload.php file so no need to identify the classes folder i think its autometically identified - Shoroar
the error shows a list of all the include directories. Apparently it isn't including classes in that list. Are you loading the config file before all of this runs? - Jonathan Kuhn
checked.i include the classes folder - Shoroar
Is this something you added after the question? I ask because the error specifically says include_path='.;C:\xampp\php\PEAR'. I don't see classes in there. Perhaps you should update the error if it has changed. - Jonathan Kuhn

4 Answers

2
votes

I know it's a bit late. I was researching on this because I had the same problem. This is what i tried and it worked for me.

// this code is for the inc/autoload.php file.

define('__ROOT__', dirname(dirname(__FILE__)));
require_once(dirname(__FILE__) . "/config.php");

//require_once(inc/config.php);


function __autoload($className){
$class = explode("_",$className);
$path = implode("/",$class).".php";
require_once($path);    

}

Please I just started learning PHP and i stand corrected by you guys. I hope this helps.

1
votes

Your Core class is apparently defined at:

C:\xampp\htdocs\completed\classes\Core.php

But you attempt to load this file:

C:\xampp\htdocs\completed\Core.php

It isn't a good idea to build a class auto-loader that works with relative paths*. I suggest you add a prefix here to build an absolute path:

 $path = implode("/", $class).".php";

E.g.:

 $path = __DIR__ . '/../classes/' . implode("/", $class).".php";

 


(*) Among other reasons, because relative paths in PHP are relative to the main script (rather than the file when path usage happens) so the source directory depends on what script you load autoload.php from.

1
votes

I've faced the same problem with yours. I think it's late for you. But may be it's helpful for others.

I didn't included the config.php file in autoload.php file

inc/autoload.php

//require_once('config.php');

I included config.php file in the index.php like this:

index.php

require_once('inc/config.php');
require_once('inc/autoload.php');

And it's worked fore me, I hope this for you, too :). Thanks for your time to read my comment.

0
votes

because in same folder

require_once('inc/autoload.php');

require_once('config.php');

so.. i think may be should like this

require_once('inc/config.php');