PHP: __autoload function doesn't ever called

Asked
Active3 hr before
Viewed126 times

5 Answers

doesnfunction
90%

If your code has an existing __autoload function then this function must be explicitly registered on the __autoload stack. This is because spl_autoload_register() will effectively replace the engine cache for the __autoload function by either spl_autoload() or spl_autoload_call()., Meta Stack Overflow , Stack Overflow Public questions & answers ,spl_autoload_register() provides a more flexible alternative for autoloading classes. For this reason, using __autoload() is discouraged and may be deprecated or removed in the future.

As DaveRandom pointed:

Autoloading is not available
if using PHP in CLI interactive mode.
load more v
88%

Name of the class to load ,spl_autoload_register() - Register given function as __autoload() implementation, You can define this function to enable classes autoloading. ,__autoload — Attempt to load undefined class

load more v
72%

so, it doesn shows 'gg' output. If i manually do __autoload('Main'); - all OK. The same for the manual require_once or include. All permissions in windows folder set to full access. Php version - PHP Version => 5.3.8;,It's not really an autoload issue. You are attempting to call a class on the root namespace.,So the problem was that the $class being returned to spl_autoload_register was the namespaceclass name, with the backslash intact. So when I instantiated a new object:,Well, at first. You need to tell the autoloader where to find the php file for a class. That's done by following the PSR-0 standard.

So, I have xampp. I tested this code on ZendServer, result the same.

< ? php
error_reporting(E_ALL);

define("ABS_PATH", 'C:xampphtdocsoopHotLine\');

      function __autoload($class_name) {
         echo 'gg';
         require_once(ABS_PATH.
            'classes\'.$class_name.'.php ');
         }

         $process = new Main('1'); ?
         >

after php.exe -a index.php i have this:

Interactive mode enabled
Fatal error: Class 'Main'
not found in C: xampphtdocsoopHotLineindex.php on line 10[Finished]
load more v
65%

PHP 7.2.0 deprecated the __autoload() magic function and recommended to use the spl_autoload_register() function instead.,It is good practice to keep each PHP class in a separate file. Also, the name of the class should be the same as the file name. For example, the Contact.php file should contain the Contact class.,Use the spl_autoload_register() function to autoload the classes, interfaces, and traits.,To use the spl_autoload_register() function, you can pass the load_model function to it as follows:

Suppose that you have the following project directory structure:

.wp - block - code {
      border: 0;
      padding: 0;
   }

   .wp - block - code > div {
      overflow: auto;
   }

   .shcb - language {
      border: 0;
      clip: rect(1 px, 1 px, 1 px, 1 px); -
      webkit - clip - path: inset(50 % );
      clip - path: inset(50 % );
      height: 1 px;
      margin: -1 px;
      overflow: hidden;
      padding: 0;
      position: absolute;
      width: 1 px;
      word - wrap: normal;
      word - break: normal;
   }

   .hljs {
      box - sizing: border - box;
   }

   .hljs.shcb - code - table {
      display: table;
      width: 100 % ;
   }

   .hljs.shcb - code - table > .shcb - loc {
      color: inherit;
      display: table - row;
      width: 100 % ;
   }

   .hljs.shcb - code - table.shcb - loc > span {
      display: table - cell;
   }

   .wp - block - code code.hljs: not(.shcb - wrap - lines) {
      white - space: pre;
   }

   .wp - block - code code.hljs.shcb - wrap - lines {
      white - space: pre - wrap;
   }

   .hljs.shcb - line - numbers {
      border - spacing: 0;
      counter - reset: line;
   }

   .hljs.shcb - line - numbers > .shcb - loc {
      counter - increment: line;
   }

   .hljs.shcb - line - numbers.shcb - loc > span {
      padding - left: 0.75 em;
   }

   .hljs.shcb - line - numbers.shcb - loc::before {
      border - right: 1 px solid #ddd;
      content: counter(line);
      display: table - cell;
      padding: 0 0.75 em;
      text - align: right; -
      webkit - user - select: none; -
      moz - user - select: none; -
      ms - user - select: none;
      user - select: none;
      white - space: nowrap;
      width: 1 % ;
   }.├──index.php└── models└── Contact.phpCode language: CSS(css)
load more v
75%

__autoload() function,As of PHP 7.2.0 the __autoload()function has been deprecated. Now it is recommended to use the spl_autoload_register for that purpose instead.,Registering multiple autoloader functions,__autoload(), a magic function, is automatically invoked by the PHP when we instantiate a class that has not already been loaded. For example:

An autoloader is a function that takes a class name as an argument and then includes the file that contains the corresponding class, for example:

function autoloader($class) {
   $path = $DOCUMENT_ROOT.
   '/classes/';
   require $path.$class.
   '.php';
}

__autoload(), a magic function, is automatically invoked by the PHP when we instantiate a class that has not already been loaded. For example:

function __autoload($class) {
   $path = $DOCUMENT_ROOT.
   '/classes/';
   require_once $path.$class.
   '.php';
}
$myCls = new myClass();

The spl_autoload_register() allows to create an autoload chain, a series of functions that can be called to try and load a class or interface:

< ? php

function loadModel($class) {
   $path = $DOCUMENT_ROOT.
   '/models/';
   require_once $path.$class.
   '.php';
}

function loadView($class) {
   $path = $DOCUMENT_ROOT.
   '/views/';
   require_once $path.$class.
   '.php';
}
}

spl_autoload_register('loadModel');
spl_autoload_register('loadView');

How often have you seen code like this at the top of your PHP files?

< ? php
require 'file_1.php';
require 'file_2.php';
require 'file_3.php';

< ? php
namespace classes;
class home {
   public
   function get() {
      return 'classes/home.php';
   }
}

classes/all.php

< ? php
namespace classes;
class all {
   public
   function get() {
      return 'classes/all.php';
   }
}

models/home.php

< ? php
namespace models;
class home {
   public
   function get() {
      return 'models/home.php';
   }
}

models/all.php

< ? php
namespace models;
class all {
   public
   function get() {
      return 'models/all.php';
   }
}

views/home.php

< ? php
namespace views;
class home {
   public
   function get() {
      return 'views/home.php';
   }
}

classes/all.php

< ? php
namespace views;
class all {
   public
   function get() {
      return 'views/all.php';
   }
}
<?php

spl_autoload_register();
$home = new classes\home();
echo $home->get() . <br>;

$home = new views\home();
echo $home->get(). <br>;

$home = new models\home();
echo $home->get(). <br>;

$all = new classes\all();
echo $all->get(). <br>;

$all = new views\all();
echo $all->get(). <br>;

$all = new models\all();
echo $all->get();

/*Output
classes/home.php
views/home.php
models/home.php
classes/all.php
views/all.php
models/all.php*/
load more v

Other "doesn-function" queries related to "PHP: __autoload function doesn't ever called"