Answers for "autoloading classes"

PHP
0

autoloading classes

improved version : 
<?php
spl_autoload_register(function($className) {
	$file = __DIR__ . '\\' . $className . '.php';
	$file = str_replace('\\', DIRECTORY_SEPARATOR, $file);
	if (file_exists($file)) {
		include $file;
	}
});
Posted by: Guest on July-21-2020
0

autoloading classes

inside root directory create a autoloader.php file and add the following code :
------------------------------------------------------------------------------
<?php
spl_autoload_register(function($className) {
	$file = $className . '.php';
	if (file_exists($file)) {
		include $file;
	}
});

inside your usage file : 
--------------------------
<?php
include 'autoload.php';

$circle = new Circle;
$square = new Square;
Posted by: Guest on July-21-2020

Browse Popular Code Answers by Language