Jump to: navigation, search

PHP Autoloading

From w3cyberlearnings
  • Some people calls autoloading class, dynamic loading class, or Loading Files and Classes Dynamically.
  • This is good because you don't need to manually include the PHP files that you need to use.
  • We create the config.php file which includes PHP method to help you load files dynamically

In this tutorial, you will learn how to develop multiple PHP applications and using the autoloading concept. Within each application it is the index.php file that uses as a main index for the application level.

Contents

Create A Simple Application

I list all files and folder structure here. This is the sample application that we are going to develop and we use autoloading to load PHP files dynamically.

File Structures

    /app_1
    /app_2
    /classes
    /config.php
    /index.php  
config.php file

This config.php has the same level as app_1, app_2, classes folders, and the index.php file. i.e,

 /config.php
 /app_1
 /app_2
 /classes
 /index.php
<?php

function __autoload($classname) {
	$possible_file = array(
		 'classes/' . $classname . '.php',
		 '../classes/' . $classname . '.php',
		 '../../classes/' . $classname . '.php',
		 
		 'app_1/classes/' . $classname . '.php',
		 '../app_1/classes/' . $classname . '.php',
		 '../../app_1/classes/' . $classname . '.php',
		 
		 'app_2/classes/' . $classname . '.php',
		 '../app_2/classes/' . $classname . '.php',
		 '../../app_2/classes/' . $classname . '.php',
		 
		 $classname . '.php'
	);
	foreach ($possible_file as $file) {
		if(file_exists($file)) {
			require_once($file);
			return true;
		}
	}
	return false;
}
?>
config.php for application level

In each application, we need the config.php file. In the config.php file we have included all the path to the PHP programs that we may need to use in our application.

 
    /app_1/config.php
    /app_2/config.php

config.php in the application levels

<?php

function __autoload($classname) {
	$possible_file = array(
		 'classes/' . $classname . '.php',
		 '../classes/' . $classname . '.php',
		 '../../classes/' . $classname . '.php',
		 
		 'app_1/classes/' . $classname . '.php',
		 '../app_1/classes/' . $classname . '.php',
		 '../../app_1/classes/' . $classname . '.php',
		 
		 'app_2/classes/' . $classname . '.php',
		 '../app_2/classes/' . $classname . '.php',
		 '../../app_2/classes/' . $classname . '.php',
		 
		 $classname . '.php'
	);
	foreach ($possible_file as $file) {
		if(file_exists($file)) {
			require_once($file);
			return true;
		}
	}
	return false;
}

?>

config.php for class level

We place this config.php in the class level under the app_1 and app_2. i.e,

    /app_1/classes/config.php 
    /app_2/classes/config.php 
<?php

function __autoload($classname) {
	$possible_file = array(
		 'classes/' . $classname . '.php',
		 'app_1/classes/' . $classname . '.php',
		 'app_2/classes/' . $classname . '.php',

		 '../../classes/' . $classname . '.php',
		 '../classes/' . $classname . '.php',
		 '../app_1/classes/' . $classname . '.php',
		 '../app_2/classes/' . $classname . '.php',

		 $classname . '.php'
	);
	foreach ($possible_file as $file) {
		if(file_exists($file)) {
			require_once($file);
			return true;
		}
	}
	return false;
}

?>

app_1 folder

    /app_1/classes
    /app_1/classes/app_1.php
    /app_1/classes/config.php
    /app_1/config.php
    /app_1/index.php
/app_1/classes/app_1.php
<?php

require_once 'config.php';

class app_1 extends Person{
	//put your code here
	public function sayHi() {
		echo "app 1";
	}
}

?>

/app_1/index.php
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title></title>
	</head>
	<body>
		<?php
			require_once 'config.php';

			$aa = new app_1();
			$aa->sayHi();
			
			$pp = new Person();
			$pp->sayHi();
		?>
	</body>
</html>

app_2 folder

    /app_2/classes
    /app_2/classes/app_1.php
    /app_2/classes/config.php
    /app_2/config.php
    /app_2/index.php
/app_2/classes/app_1.php
<?php

require_once 'config.php';

class app_1 extends Person {
	//put your code here
	public function sayHi() {
		echo 'app 2 ';
	}
}

?>

/app_2/index.php
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title></title>
	</head>
	<body>
		<?php
			require_once 'config.php';

			$aa = new app_1();
			$aa->sayHi();
			
			$pp = new Person();
			$pp->sayHi();
			
			//$aa2 = new app_2();
			//$aa2->sayHi();
		?>
	</body>
</html>

classes folder

    /classes
    /classes/Person.php
    /classes/app_1.php
    /classes/config.php
/classes/config.php

This is the autoloading file which is dynamically loaded the PHP files from app_1, app_2, and classes folders.

<?php

function __autoload($classname) {
	$possible_file = array(
		 'classes/' . $classname . '.php',
		 '../classes/' . $classname . '.php',
		 '../../classes/' . $classname . '.php',
		 
		 'app_1/classes/' . $classname . '.php',
		 '../app_1/classes/' . $classname . '.php',
		 '../../app_1/classes/' . $classname . '.php',
		 
		 'app_2/classes/' . $classname . '.php',
		 '../app_2/classes/' . $classname . '.php',
		 '../../app_2/classes/' . $classname . '.php',
		 
		 $classname . '.php'
	);
	foreach ($possible_file as $file) {
		if(file_exists($file)) {
			require_once($file);
			break;
		}
	}
}

?>
/classes/Person.php
<?php

class Person {
	public function sayHi() {
		echo "good ";
	}
}

?>

/classes/app_1.php
<?php

require_once 'config.php';

class app_1 extends Person {
	public function sayHi() {
		echo 'main class';
	}
}

?>

/index.php file

<?php
require_once 'config.php';

$pp = new Person();
$pp->sayHi();

$aa = new app_1();
$aa->sayHi();

?>

Navigation
Web
SQL
MISC
References