Skip to content

Instantly share code, notes, and snippets.

@AbmSourav
Created November 17, 2019 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AbmSourav/94b0ae6806a77e0add03dcf70071c3b7 to your computer and use it in GitHub Desktop.
Save AbmSourav/94b0ae6806a77e0add03dcf70071c3b7 to your computer and use it in GitHub Desktop.
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