PHP Classes

ghz PHP Autoloader: Autoload classes scanning files in directories

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 89 All time: 9,977 This week: 36Up
Version License PHP version Categories
ghzautoloader 1.0.0GNU General Publi...5PHP 5, Libraries
Description 

Author

This package can autoload classes scanning files in directories.

It takes a list of given directories where class files are located and checks the files with PHP scripts with common file extensions like .php, .inc and .lib.

The autoloader supports different conventions to locate scripts to be loaded that contain class files by mapping the requested class name to a file name or using PSR-4 convention to map class names that may include namespaces.

Picture of adriano ghezzi
  Performance   Level  

 

Example

<?php
/**
 * Created by PhpStorm.
 * User: adriano
 * Date: 25/02/19
 * Time: 18.33
 */


/**
 * suppose to have the folloqing scenario
 * all your classe are in
 ...myPackg/
??? ghzAutoloader.php
??? samples
??? defaultImplementation.php
??? myPckg
?   ??? Core
?   ?   ??? classOne.php
?   ??? Helpers
?   ??? helperOne.php
??? psr4sample.php

 */
require_once "../ghzAutoloader.php";

/* OPTION 1 you can define one top-level namespace -> 'myPckg'
and namespace classes accordingly as in class helperOne
*/
/*$namespacesBasedirs=['myPckg'=>[__DIR__ . DIRECTORY_SEPARATOR . 'myPckg' . DIRECTORY_SEPARATOR]];
ghzAutoloader::Psr4Implementation($namespacesBasedirs,false,true);

$one=new \myPckg\Core\classOne();
$h=new \myPckg\Helpers\helperOne();*/

/* option2 you can define two top-level name spaces
myPckg and Helpers
and namespace classes accordingly as in class helperTwo
*/
$namespacesBasedirs=[
                   
'myPckg'=>[__DIR__ . DIRECTORY_SEPARATOR . 'myPckg' . DIRECTORY_SEPARATOR],
                   
'Helpers'=>[__DIR__ . DIRECTORY_SEPARATOR . 'myPckg' . DIRECTORY_SEPARATOR.'Helpers' . DIRECTORY_SEPARATOR],
    ];
ghzAutoloader::Psr4Implementation($namespacesBasedirs,false,true);

$one=new \myPckg\Core\classOne();
$h=new \myPckg\Helpers\helperOne();
$h2=new \Helpers\helperTwo();


  Files folder image Files (10)  
File Role Description
Files folder imagesamples (3 files, 2 directories)
Plain text file ghzAutoloader.php Class package file

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:89
This week:0
All time:9,977
This week:36Up