Skip to content

Instantly share code, notes, and snippets.

@AbmSourav

AbmSourav/autoload

Created Nov 17, 2019
Embed
What would you like to do?
PHP Class autoload
<?php
// classes/greatings.php
namespace Classes;
class Greatings {
function hello() {
return "Successfully called Greatings";
}
}
?>
<?php
//index.php
namespace Classes;
function my_autoloader($class) {
if ( false === strpos( $class, __NAMESPACE__ ) ) {
return;
}
$class_name = preg_replace( '/^' . __NAMESPACE__ . '\\\/', '', $class );
require_once __DIR__ . '/classes/' . strtolower( $class_name ) . '.php';
}
spl_autoload_register(__NAMESPACE__.'\my_autoloader');
$greatings = new Greatings;
echo $greatings->hello();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.