Code Monkey home page Code Monkey logo

phpclassloader's Introduction

PHP Class Loader

This is a simple class loader that I created for a project I was working on. It is not a full fledged class loader, but it does the job for me. I am sharing it here in case anyone else finds it useful.

e.g:

function loadClasses()
{
	$base = 'FormEditor';
	$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__));
	$files = [];

	foreach($iterator as $iter) {
		if($iter->isFile()) {
			$files[] = $iter->getPathname();
		}
	}

	$loaded_classes  = [];

	foreach ($files as $file) {
		if(file_exists($file)) {
			if(strpos($file, 'Classes') != false) {
				require_once($file);
			} else {
				require_once($file);
				$file = str_replace(__DIR__, '', $file);
				$file = preg_replace('/\//', '\\', $file);
				$file = str_replace('.php', '', $file);
				$loaded_classes[] = $base . $file;
			}
		}
	}

	$order = [];

	foreach($loaded_classes as $class) {
		
		if(class_exists($class)) {
			$init = new $class();

			if(property_exists($init, 'priority')) {
				$order[] = [
					'priority' => $init->priority,
					'instance' => $init
				];
			} else {
				$order[] = [
					'priority' => 0,
					'instance' => $init
				];
			}
		} 
	}

	usort($order, function($a, $b) {
		return $a['priority'] - $b['priority'];
	});

	foreach($order as $class) {
		$class['instance']->init();
	}
}

The order in which we load classes is important. If we create abstract classes then they must be loaded first. In the above example you can see that I designated a Classes folder as the source of our project's abstract classes and it is the very first to be loaded.

We define an init function in our Base.php abstract class and in our example we call the init function on all the ordered classes. So all the classes that are being loaded extend the Base class, which is also the reason we need to load it before anything else.

NOTE: the priority property is optional. If you do not define it, then the class will automatically be given a priority of 0.

phpclassloader's People

Contributors

makanamakesstuff avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.