stave.dev

Class Loading

One rule maps a class name to a file. The namespace is the path: no class map, no require, no composer.json entry.

The one rule

Application::classFile() in core/Application.php takes a class name and returns the file it lives in, or null. Everything that loads a Zero class — a request, a command-line script, the test bootstrap — asks this method. The first namespace prefix that matches decides the folder:

Namespace Folder
Zero\Core\Attribute\core/attribute/
Zero\Core\core/
Zero\Lib\core/lib/
Zero\Entity\core/entity/, then the site's entity/
Zero\Aux\aux/
Zero\App\Lib\app/lib/
Zero\Core\Module\{Name}\core's module of that name, core/modules/{Name}/ (Modules::corePath())
Zero\Module\{Name}\the site's module of that name, modules/{Name}/ (Modules::sitePath())

Folders are relative to the folder that holds core/. The site folder itself may carry any name. The two module rows are checked before the others, so Zero\Core\Module\Auth is a bundled module's class and never a file in core/. Zero\Core\Module itself, with nothing after it, is core/Module.php, the base class every module extends.

The namespace says whose module a class belongs to. A site that overrides a bundled module has Zero\Module\Auth in its own modules/Auth/, and core's Zero\Core\Module\Auth still loads from core/modules/Auth/, so the one can extend the other. See Modules and Bundled Modules.

The remaining segments

After the prefix, the last segment is the class and its file is {Class}.php. Each segment before it is a folder, tried three ways in this order:

  1. As written — Zero\Lib\Net\Cidr is core/lib/Net/Cidr.php.
  2. As a submodule (module classes only) — submodule/{Segment}/.
  3. Lowercased — Model is model/, Lib is lib/.

For a module class, the class itself may also be a submodule: when {Class}.php is not in the folder reached, submodule/{Class}/{Class}.php is tried.

Class File
Zero\Core\Databasecore/Database.php
Zero\Core\Attribute\RequireLogincore/attribute/RequireLogin.php
Zero\Lib\Mail\Mailcore/lib/Mail/Mail.php
Zero\Module\Blogmodules/Blog/Blog.php
Zero\Module\Blog\Model\Postmodules/Blog/model/Post.php
Zero\Module\Blog\Lib\Feedmodules/Blog/lib/Feed.php
Zero\Module\Blog\Draftsmodules/Blog/submodule/Drafts/Drafts.php
Zero\Module\Blog\Drafts\Model\Draftmodules/Blog/submodule/Drafts/model/Draft.php
Zero\Core\Module\Authcore/modules/Auth/Auth.php
Zero\Core\Module\Auth\AccessRequestcore/modules/Auth/submodule/AccessRequest/AccessRequest.php
Zero\Core\Module\Push\Lib\PushHelpercore/modules/Push/lib/PushHelper.php
Zero\App\Lib\Pricingapp/lib/Pricing.php

Only the segments are mapped. A class whose own name contains Model or Lib loads like any other.

Two checks

Entity classes

Zero\Entity\ has two bases. core/entity/ is tried first; it holds the entity layer itself. When the class is not there, the site's entity/ folder is tried — or the folder the constant ENTITY_SITE_DIR names, when it is defined. That is where a site's own entity classes live.

The framework's class wins

Because core/entity/ is tried first, a site class with the same name as a framework entity class is never loaded. See Entity Framework for the layer itself.

Models and lib/ in a module

Folder Namespace Who uses it
modules/{Name}/model/ Zero\Module\{Name}\Model The module itself
modules/{Name}/lib/ Zero\Module\{Name}\Lib The module, and any other module

A bundled module's folders follow the same pattern under Zero\Core\Module\{Name}\: core/modules/Push/lib/ is Zero\Core\Module\Push\Lib.

Logic another module needs goes in the providing module's lib/. Other modules call a provider's lib/, never its model/, so the provider's rules apply wherever its data changes (core/docs/ZERO.md, "Sharing logic between modules").

<?php
namespace Zero\Module\Blog\Lib;

use Zero\Module\Blog\Model\Post;

class Feed {

    /* Takes what it needs as arguments rather than reading the session. */
    public function byAuthor(int $authorId): array {
        return Post::allBy('author_id', $authorId);
    }
}
<?php
namespace Zero\Module;

use Zero\Module\Blog\Lib\Feed;   // Blog's lib/, not its model/

class Dashboard extends \Zero\Core\Module {

    public function author($id) {
        $this->data = ['posts' => (new Feed())->byAuthor((int) $id)];
        $this->respond($this->viewPath . 'author.php');
    }
}

Code that belongs to the site but to no module goes in app/lib/ as Zero\App\Lib\{Class}. Zero\Aux\ maps to an aux/ folder at the site root. The libraries that belong to no site are in core/lib/; see Libraries. Models are covered in Models & Database.

What a request registers

Application::registerAutoloaders() runs once, from the site's index.php. In order:

  1. Zero's own — Application::autoloader(), which loads the file classFile() returns.
  2. Yours — the callable, or list of callables, passed as the argument.
  3. Composer's — vendor/autoload.php from ROOT_PATH, then from ZERO_ROOT, each when the file exists.
// No argument: Zero's loader, then Composer's when there is a vendor/ folder.
$app->registerAutoloaders();

// One more loader of your own, or a list of them.
$app->registerAutoloaders(function (string $class) {
    // ...
});

Composer loads third-party packages only. No class of the framework or of a module needs an entry in composer.json, and a site with no vendor/ folder loads every Zero class just the same.

A Zero class that cannot be placed is logged

When classFile() returns null for a name that starts with Zero\, Application::autoloader() writes Autoloader failed to place {class} to the log as a warning and leaves the class to the next autoloader. A typo in a namespace shows up there. See Security & Logging for where the log is.

Scripts, cron jobs and tests

Code that does not run through index.php includes core/autoload.php. One line:

<?php
require __DIR__ . '/../../../core/autoload.php';

use Zero\Module\Blog\Lib\Feed;   // loads by the same rule a request uses

The file registers a loader that calls Application::classFile(), points Modules at the site's modules/ and at core/modules/, and includes vendor/autoload.php when the site has one. Two differences from a request:

The test bootstrap, core/tests/bootstrap.php, includes the same file, so a test names a class in a use line and needs no require_once (core/tests/README.md).