php - Autoloader issue not returning Class -


problem?

bootloader (autoloader) not seem working properly, or i'm missing something. here simplified code.

the code below returns

class "skeleton" not exist.

on index.php file.

index.php

<?php  include 'bootloader.php'; use skeleton\html\loginheader; $tool = new skeleton/html/loginheader(); 

bootloader.php

<?php  function boot($classname) {         $filename = '';         $namespace = '';          // sets include path "src" directory         $includepath = dirname(__file__).directory_separator.'src';          if (false !== ($lastnspos = strripos($classname, '\\'))) {             $namespace = substr($classname, 0, $lastnspos);             $classname = substr($classname, $lastnspos + 1);             $filename = str_replace('\\', directory_separator, $namespace) . directory_separator;         }         $filename .= str_replace('_', directory_separator, $classname) . '.php';         $fullfilename = $includepath . directory_separator . $filename;          if (file_exists($fullfilename)) {             require $fullfilename;         } else {             echo 'class "'.$classname.'" not exist.';         }     }     spl_autoload_register('boot'); // registers autoloader 

src/skeleton/html/loginheader.php

<?php  namespace skeleton\html;  class loginheader () {     echo "<h1>login header ok!</h1>"; } 

a couple issues here:

1) line/section not right:

class loginheader () { 

should be:

class loginheader     {         public function __construct()             {                 echo "<h1>login header ok!</h1>";                 ...etc 

2) not assigning class correctly. have:

$tool = new skeleton/html/loginheader(); 

should backslashes:

            --------v----v $tool = new skeleton\html\loginheader(); 

once fix backslashes, syntax error on class page noted, autoloader works fine.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -