Created
November 17, 2019 17:40
-
-
Save AbmSourav/94b0ae6806a77e0add03dcf70071c3b7 to your computer and use it in GitHub Desktop.
PHP Class autoload
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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