Php Autoload function

What is Php Autoload function?

__autoload() is a magic method, means it calls automatically when you try create an object of the class and if the PHP engine doesn’t find the class in the script it’ll try to call __autoload() magic method.
void __autoload ( string $classname )

Note:-(i) Autoload function does not return value.
(ii) class name and filename must be same.

Example:-


<?php
function __autoload($class_name) {
    include $class_name . '.php';
}

$obj1  = new MyClass1();
$obj2 = new MyClass2(); 
?>

What is the reason we use autoload function?

If we have 4 files
(a) tcs.php which has tcs information.
(b) oracle.php which oracle information.
(c) google.php which google information.
(d) companyinfo.php which has company information of all three company.

(i) first is tcs.php which has


<?php
class tcs{
public function which_type()
{
return 'TCS is a  consulting and business solutions company';
}
public function number_employees()
{
return 'Aproximate 3 lakh';
}
public function fresher_package_for_employee()
{
return '3.2 lakh per annum';
}
} 
?>

(ii) second is oracle.php


<?php
class oracle{
public function which_type()
{
return 'Oracle is a  database company';
}
public function number_employees()
{
return 'Aproximate 2 lakh';
}
public function fresher_package_for_employee()
{
return '5.2 lakh per annum';
}
}
?>

(iii) third is google.php


<?php
class google{
public function which_type()
{
return 'Google is a  search base company';
}
public function number_employees()
{
return 'Aproximate 2.5 lakh';
}
public function fresher_package_for_employee()
{
return '6.2 lakh per annum';
}
}
?>

In companyinfo.php we include all three files of company after that create a objects


<?php
include('tcs.php');
include('oracle.php');
include('google.php');

$tcs_obj= new tcs;
echo $tcs_obj->which_type(); 
echo $tcs_obj->number_employees(); 
echo $tcs_obj->fresher_package_for_employee();
?>
Output:- TCS is a consulting and business solutions
companyAproximate 3 lakh
3.2 lakh per annum

<?php
$orac_obj= new oracle;
echo $orac_obj->which_type();
echo $orac_obj->number_employees();
echo $orac_obj->fresher_package_for_employee();
?>
Output:- Oracle is a database company
Aproximate 2 lakh
5.2 lakh per annum

<?php
$google_obj= new google;
echo $google_obj->which_type();
echo $google_obj->number_employees();
echo $google_obj->fresher_package_for_employee();
?>
Output:- Google is a search base company
Aproximate 2.5 lakh
6.2 lakh per annum

In this example we include three files in the companyinfo.php but if want to include more than 20 files then it is a hard nut to crack to include the files..
So multiple include file problem, solve with the help of autoloading function.

so we used autoload function in the companyinfo.php


<?php
function __autoload($class_name)
{
include $class_name.'.php';
}
$tcs_obj= new tcs;
$orac_obj= new oracle;
$google_obj= new google;
?>

then create a object of the 3 classes then automatically include three files in this file.
autoload