Back to questions
JuniorTheoryCommon
What does the program print?
<?php
$map = ['App\\' => 'src/'];
$class = 'App\\Http\\Kernel';
foreach ($map as $prefix => $dir) {
if (str_starts_with($class, $prefix)) {
$rest = substr($class, strlen($prefix));
echo $dir, str_replace('\\', '/', $rest), '.php', "\n";
}
}src/Http/Kernel.php
- Expecting the namespace prefix to stay in the path: PSR-4 replaces the prefix
App\with its directorysrc/, soApp\Http\Kernellives insrc/Http/Kernel.php. - Reading
'\\'as two backslashes: in single quotes\\is one backslash, sostr_replace('\\', '/', …)turns every namespace separator into/.
- Which command rebuilds Composer's autoloader after you add a prefix to
composer.json? - Which file must declare the class
App\Models\Userunder the same map?