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:
- As written —
Zero\Lib\Net\Cidriscore/lib/Net/Cidr.php. - As a submodule (module classes only) —
submodule/{Segment}/. - Lowercased —
Modelismodel/,Libislib/.
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\Database | core/Database.php |
Zero\Core\Attribute\RequireLogin | core/attribute/RequireLogin.php |
Zero\Lib\Mail\Mail | core/lib/Mail/Mail.php |
Zero\Module\Blog | modules/Blog/Blog.php |
Zero\Module\Blog\Model\Post | modules/Blog/model/Post.php |
Zero\Module\Blog\Lib\Feed | modules/Blog/lib/Feed.php |
Zero\Module\Blog\Drafts | modules/Blog/submodule/Drafts/Drafts.php |
Zero\Module\Blog\Drafts\Model\Draft | modules/Blog/submodule/Drafts/model/Draft.php |
Zero\Core\Module\Auth | core/modules/Auth/Auth.php |
Zero\Core\Module\Auth\AccessRequest | core/modules/Auth/submodule/AccessRequest/AccessRequest.php |
Zero\Core\Module\Push\Lib\PushHelper | core/modules/Push/lib/PushHelper.php |
Zero\App\Lib\Pricing | app/lib/Pricing.php |
Only the segments are mapped. A class whose own name contains Model or Lib loads like any other.
Two checks
- Identifiers. Every namespace segment must be a PHP identifier (
Application::isQualifiedName()). A name carrying a dot or a slash maps to nothing. - Containment. The file found is resolved with
realpath()and must lie inside the folder its rule names — a module's own folder for a module class. A symlink that leads outside is not loaded.
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.
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:
- Zero's own —
Application::autoloader(), which loads the fileclassFile()returns. - Yours — the callable, or list of callables, passed as the argument.
- Composer's —
vendor/autoload.phpfromROOT_PATH, then fromZERO_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.
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:
- A class that is not found is left to the next autoloader without a log line.
- No config file is read, so no
.iniconstant is defined. A script that needs one defines it or reads the file itself.
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).