Last active
December 13, 2024 06:02
-
-
Save daveh/337783ef765f8f33b7efff9ff7dd8fe8 to your computer and use it in GitHub Desktop.
Loading Classes Automatically in PHP (code to accompany https://youtu.be/93pCiZT99Ks)
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
{ | |
"autoload": { | |
"psr-4": { | |
"": "src/" | |
} | |
} | |
} |
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 | |
use Framework\Router; | |
/* | |
spl_autoload_register(function(string $class) { | |
$path = 'src/' . str_replace('\\', '/', $class) . '.php'; | |
require $path; | |
}); | |
*/ | |
require 'vendor/autoload.php'; | |
$user = new App\User; | |
echo $user->getName(); | |
$product = new Database\Models\ProductModel; | |
echo $product->getId(); | |
$router = new Router; | |
echo get_class($router); | |
$order = new Database\Models\OrderModel; | |
echo get_class($order); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment