Skip to content

Instantly share code, notes, and snippets.

@toopay
Created October 15, 2012 06:51
Show Gist options
  • Save toopay/3891122 to your computer and use it in GitHub Desktop.
Save toopay/3891122 to your computer and use it in GitHub Desktop.
SplClassLoader Example
<?php
// Let say we have these directory structure, that follow PSR-0 spec :
//
// system
// - framework
// - Http
// - Request.php
// - Response.php
// test.php
// SplClassLoader.php
//
// Then we could utilize it as :
require_once('SplClassLoader.php');
$loader = new SplClassLoader('framework', 'system/framework');
$loader->register();
// Now we could call any classes within
// framework namespace without the need to include each files/classes
// as easy as :
$request = new Http\Request();
@WebSofter
Copy link

How to work this? I am is not understand.

My project have tree:

//system
//-framework
//--CForm.php
// index.php
// SplClassLoader.php

//the CForm.php have code

"; } public function endForm() { echo ""; } ``` } //And i using this code in index.php for loading my class: register(); //Folowing is example of using loader use CForm; $object1=new CForm(); $object1->startForm(); $object1->endForm(); //But i ave error Class 'CForm' not found in C:\OpenServer\domains\Formulator.loc\index.php

@thlib
Copy link

thlib commented Dec 1, 2013

Could you elaborate on how the contents of Request.php and Response.php might look like?

@techbly
Copy link

techbly commented Apr 15, 2014

$loader = new SplClassLoader('framework', 'system/framework');

use it like this

$loader = new SplClassLoader('framework', 'system');

@seanlindo
Copy link

What are the contents of Request.php or Response.php? This would be helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment