PSR-4 Autoloader

suggest change

PSR-4 is an accepted recommendation that outlines the standard for autoloading classes via filenames. This recommendation is recommended as the alternative to the earlier (and now deprecated) PSR-0.

The fully qualified class name should match the following requirement:

\<NamespaceName>(\<SubNamespaceNames>)*\<ClassName>

Thus the final class name would be Alphabet\Google\AdWord\KeywordPlanner. The fully qualified class name should also translate into a meaningful file path therefore Alphabet\Google\AdWord\KeywordPlanner would be located in [path_to_source]/Alphabet/Google/AdWord/KeywordPlanner.php

Starting with PHP 5.3.0, a custom autoloader function can be defined to load files based on the path and filename pattern that you define.

# Edit your php to include something like:
spl_autoload_register(function ($class) { include 'classes/' . $class . '.class.php';});

Replacing the location (‘classes/’) and filename extension (’.class.php’) with values that apply to your structure.

Composer package manager supports PSR-4 which means, if you follow the standard, you can load your classes in your project automatically using Composer’s vendor autoloader.

# Edit the composer.json file to include
{
    "autoload": {
        "psr-4": {
            "Alphabet\\": "[path_to_source]"
        }
    }
}

Regenerate the autoloader file

$ composer dump-autoload

Now in your code you can do the following:

<?php

require __DIR__ . '/vendor/autoload.php';
$KeywordPlanner = new Alphabet\Google\AdWord\KeywordPlanner();

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


PSR:
* PSR
* PSR-4 Autoloader

Table Of Contents
2 Arrays
4 Types
10 Cookies
14 JSON
15 SOAP
17 cURL
19 XML
21 Traits
35 UTF-8
36 URLs
38 PHPDoc
41 Loops
44 Closur
72 YAML
77 Cache
78 Streams
81 PDO
82 SQLite3
83 Sockets
87 MongoDB
93 IMAP
94 Redis
95 Imagick
102 APCu
108 PSR