1
I intend to make my code cleaner and therefore avoid using include
or include_once
all page start. I would not like to use frameworks that already do this work as Windows for example. I want to create the function "from the Scratch".
So here are 2 different ways to use autoload functions to call a class when trying to instantiate an object.
spl_autoload_register(function ($class_name) {
include './class/'.$class_name . '.php';
});
================== ou ========================
function __autoload($className) {
$className = str_replace("..","", $className);
require_once ("./class/$className.php");
}
I’m using the function on the page index.php
.
My question is whether there is a sublevel constraint for it to work. That is, if a class is called in a file file.php ($myObj=new myclass();)
inside the directory dir1/dir2/dir3/file.php
she would continue to recognize the path until root/class/myclass.php
? Is there any limit ?
Thank you for the explanation of how to use, also would like to know.
– Florida