Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php custom autoload

<?php
//custom autoload without composer
$to_map = [
    'App' =>  'app/',
    'Core' => 'Core/',
];

foreach($to_map as $prefix => $base_dir)
{
    spl_autoload_register(function ($class) use ($prefix, $base_dir) {

        $base_dir = __DIR__ . "/{$base_dir}";

        $len = strlen($prefix);
        if (strncmp($prefix, $class, $len) !== 0)
        {
            return;
        }

        $relative_class = substr($class, $len);

        $file = $base_dir . str_replace('', '/', $relative_class) . '.php';

        if (file_exists($file))
        {
            require $file;
        }
    });
}
Comment

autoload.php

The introduction of spl_autoload_register() gave programmers 
the ability to create an autoload chain, 
a series of functions that can be called to try and load a class or interface. 

For example:

<?php
function autoloadModel($className) {
    $filename = "models/" . $className . ".php";
    if (is_readable($filename)) {
        require $filename;
    }
}

function autoloadController($className) {
    $filename = "controllers/" . $className . ".php";
    if (is_readable($filename)) {
        require $filename;
    }
}

spl_autoload_register("autoloadModel");
spl_autoload_register("autoloadController");
Comment

PREVIOUS NEXT
Code Example
Php :: laravel echo html 
Php :: how to acces sql with php 
Php :: select multiple option in laravel 
Php :: how to install laravel 
Php :: Converting timestamp to time ago in PHP 
Php :: php unique associative nested array by value 
Php :: php super 
Php :: laravel swagger install 
Php :: php if boolean check 
Php :: delete and return response and nocontent laravel 
Php :: php json data to array 
Php :: laravel get all old input 
Php :: fakher ul islam khan 
Php :: foreign key cosntraint laravel 
Php :: unset php return array 
Php :: convert multdimentional array in array in php 
Php :: mysqli exception handling 
Php :: throw 403 laravel 
Php :: php laravel dump 
Php :: Delete a single record in laravel 5 
Php :: job with queue name 
Php :: validate columns laravel excel 
Php :: laravel validation types 
Php :: php random filename generator 
Php :: laravel model set new attribute 
Php :: script inside php 
Php :: php mail template 
Php :: like %% inside the string php 
Php :: session start php 
Php :: is null php 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =