ScotlandPHP 2019

(PHP 5, PHP 7)

Warning

This feature has been DEPRECATED as of PHP 7.2.0. Relying on this feature is highly discouraged.

__autoload ( string $class ) : void You can define this function to enable classes autoloading. ### Parameters class Name of the class to load ### Return Values No value is returned. ### See Also add a note ### User Contributed Notes 11 notes 54 qeremy 6 years ago Even I have never been using this function, just a simple example in order to explain it; ./myClass.php <?php class myClass { public function __construct() { echo "myClass init'ed successfuly!!!"; } } ?> ./index.php <?php // we've writen this code where we need function __autoload($classname) {

$filename = "./".$classname .".php";
include_once(
$filename); } // we've called a class ***$obj = new myClass();
?>

*** At this line, our "./myClass.php" will be included! This is the magic that we're wondering... And you get this result "myClass init'ed successfuly!!!".

So, if you call a class that named as myClass then a file will be included myClass.php if it exists (if not you get an include error normally). If you call Foo, Foo.php will be included, and so on...

And you don't need some code like this anymore;

<?php
include_once("./myClass.php");
include_once(
"./myFoo.php");
include_once(
"./myBar.php");

$obj = new myClass();$foo = new Foo();
$bar = new Bar(); ?> Your class files will be included "automatically" when you call (init) them without these functions: "include, include_once, require, require_once". 41 ohcc at 163 dot com 5 years ago It is highly recommended not to use the __autoload() function any more. Now the spl_autoload_register() function is what you should consider.Sorry for the mistake in line 6 of my previous note. And below is the corrected PHP code. <?php if(!function_exists('classAutoLoader')){ function classAutoLoader($class){

$class=strtolower($class);

$classFile=$_SERVER['DOCUMENT_ROOT'].'/include/class/'.$class.'.class.php'; if( is_file($classFile)&&!class_exists($class)) include$classFile;
}
}

?>
keyboardSmasher
5 years ago

<?php
include_once("./myClass.php");
include_once(
"./myFoo.php");
include_once(
"./myBar.php");

$obj = new myClass();$foo = new Foo();
$bar = new Bar(); ?> <?php$foo
= new Foo();
$bar = new Bar(); ?> should be: <?php$foo
= new myFoo();
$bar = new myBar(); ?> eragroove at gmail dot com 5 years ago If you can keep file name and class name as same, it will be good programming practice. It helps to __autoload function to load file without checking any condition. function __autoload($class){
require_once( $class.".php"); } -4 wojciech.fornal@gmail,com 5 years ago keyboardSmasher You may or may not be right as the file name doesn't necessarily have to reflect a class name it contains (but it's usually considered a good practice). It isn't always a straightforward mapping (look at some PHP frameworks and autoload implementations). File myBar.php may contain the class: class Bar { } or it even contain the class: class Foo { } Best regards -2 dagirovdjamal at gmail dot com 1 year ago Simple autoload : function __autoload($class_name){
require 'class/' . $class_name . '.php'; } -11 Jose G 4 years ago You should use include() or require() inside __autoload() instead of include_once() or require_once(). If you reach __autoload(), then you know the file with the class definition has not been loaded yet. include() and require() are more efficient than include_once() and require_once(). -15 Ben 4 years ago Guys, this document ( i mean __autoload() ) not mentioned one special situation: if you both use __autoload() and spl_autoload_register(), the __autoload() function will never to be called. Although spl_autoload_register() documentation explained why, i decide to wrote this in case some one get confused and waste all day to figure out why. Here is some code to verify above: <?php function __autoload($class) {
}

function
}

function
}

echo
'<br/>';

-7
ohcc at 163 dot com
1 year ago
In PHP 7.2, this code will trigger a "
<?php

function
__autoload($class){ // blah blah blah } } ?> -21 romi 4 years ago <?php if(!function_exists('classAutoLoader')){ function classAutoLoader($class){

$class=strtolower($class);

$classFile=$_SERVER['DOCUMENT_ROOT'].'/include/class/'.$class.'.class.php'; if( is_file($classFile)&&!class_exists($class)) include$classFile;
}
//You have to give the class name and not the class's path for the first parameter in the class_exists()

}

?>
-33
ohcc at 163 dot com
5 years ago
It is highly recommended not to use the __autoload() function any more. Now the spl_autoload_register() function is what you should consider.
<?php

classAutoLoader($class){$class=strtolower($class);$classFile=$_SERVER['DOCUMENT_ROOT'].'/include/class/'.$class.'.class.php';
is_file($classFile)&&!class_exists($classFile)) include \$classFile;