-1

Hello i have a task to do to class the classes by its namespace. My requirement is to load the classes without including the require keyword for autoload.php.

<?php
namespace classes;
class test {
public function getPrint(){
    echo "Hello";
}
}

This is the class with namespace. Here is the code for to class functions.

require_once 'classes/autoload.php';
use classes\test2;

$dt = new test();
$dt2 = new test2();


$dt->getPrint();
echo $dt2->Okay();

I am autoloading the classes. my problem is to load the classes with using

require_once 'classes/autoload.php';

This line. I have to use this line in every code file to load the classes. What is the proper way to load the classes. So i do not have to include

require_once 'classes/autoload.php';

this in every file?

  • You will have to `require` *something somewhere*. The usual way frameworks are solving this is to have one *bootstrap* file, i.e. something that every single request gets routed through that includes all necessary (autoload-)files and sets up configurations before continuing to handle the details of the request. – deceze Jul 01 '20 at 08:35
  • Can you show me an example? @deceze – Sohail Farooq Jul 01 '20 at 08:46

2 Answers2

0

The usual way this works is to use one bootstrap file that each request goes through. This also usually requires to move routing into the PHP code; i.e. instead of example.com/foo/bar.php simply invoking the file foo/bar.php, you'd usually use pretty URLs like example.com/foo/bar, and then figure out in your PHP code what foo/bar means exactly and what it should do (though this is pretty optional and up to you to decide).

First you set up an .htacess file like this, or add an equivalent configuration in your web server config files:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ bootstrap.php

This causes your web server to invoke bootstrap.php for any and all requests whose file doesn't actually exist (i.e. css/style.css would still be served as is, but foo/bar is not an actual file and would invoke bootstrap.php instead).

In bootstrap.php you'd then require all the necessary files like auto-loaders and perhaps configuration files, and then run whatever class, function or file you want:

<?php

require_once 'classes/autoload.php';

$request = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
require "$request.php";

The above is a simple example for taking the requested URI (e.g. /foo/bar) and using it to require a PHP file. This is very naïve and potentially dangerous, so you'll want to do some more custom processing in actual use.

deceze
  • 510,633
  • 85
  • 743
  • 889
-1

You could create a file which requires all other files you want to use, including the autoload:

<?php

require_once('classes/autoload.php');
require_once('file1.php');
require_once('file2.php');
...

Then you won't need to require the autoload, as files are already appended after it.

user8555937
  • 2,161
  • 1
  • 14
  • 39
  • Your answer tells me that there is no way without require? We must have to include files to autoload the classes? – Sohail Farooq Jul 01 '20 at 08:41
  • I don't know your project structure. Each script which will access the classes from autoload must include one way or another the autoload file. Including all scripts in a single file with autoload on top of it makes more sense and resolves this problem. However it will solve the issue depending on your project structure. – user8555937 Jul 01 '20 at 08:50
  • my structure is procedural php code and i ahve to convert that code into OOP with routing includes. So, i think i have to convert it into MVC that will help me put all the scripts in same page. – Sohail Farooq Jul 01 '20 at 08:56
  • You should go for a framework such as Codeigniter or Laravel if you want to rebuild it in MVC patter. They could help you. – user8555937 Jul 01 '20 at 09:40