laminas/laminas-file
Laminas File provides file upload and transfer handling for Laminas applications. It includes HTTP upload adapters that manage uploaded files, expose metadata, and integrate with validators and filters for safe, flexible file processing.
laminas/laminas-file is a small, archived utility package. Its day-to-day value is narrow: it scans PHP files and tells you which classes are declared in them. It does not autoload, resolve PSR-4 namespaces, or register anything with Composer — it is a class-file locator, typically used by CLI tooling, legacy module discovery, or code-generation utilities.
Install it only if you are maintaining an existing consumer:
composer require laminas/laminas-file
Start by reading two files:
src/ClassFileLocator.php — the iterator that walks directories and filters to PHP files containing classes.src/PhpClassFile.php — an SplFileInfo subclass that exposes the classes found in a file.Minimal first use case — build an in-memory map of class name to file path:
use Laminas\File\ClassFileLocator;
$locator = new ClassFileLocator(__DIR__ . '/src');
$classMap = [];
foreach ($locator as $file) {
foreach ($file->getClasses() as $className) {
$classMap[$className] = $file->getPathname();
}
}
var_export($classMap);
The locator is lazy: files are inspected as you iterate. For a realistic project, run this from a CLI command or build script, not from a web request.
Build a classmap artifact once, then cache it. The locator tokenizes PHP files, so it is not cheap. Generate a JSON/PHP array classmap in CI or a console command and load that at runtime:
$classMap = json_decode(file_get_contents('var/classmap.json'), true);
Use it for interface/implementation discovery in small, controlled directories. Scan your own src/, filter the resulting class names, then verify with reflection before instantiating:
foreach ($locator as $file) {
foreach ($file->getClasses() as $class) {
if (!class_exists($class)) {
continue;
}
$reflection = new ReflectionClass($class);
if ($reflection->implementsInterface(MyHookInterface::class)) {
$hooks[] = $reflection->newInstance();
}
}
}
Do not point this at vendor/ unless you deliberately want to discover third-party classes; the scan cost grows quickly.
Wrap the locator in a small service. Keep the package behind your own interface so replacing it later is a one-file change:
final class SourceClassMap
{
public function build(string $directory): array
{
$map = [];
foreach (new ClassFileLocator($directory) as $file) {
foreach ($file->getClasses() as $class) {
$map[$class] = $file->getRealPath();
}
}
return $map;
}
}
Use PhpClassFile as a normal SplFileInfo. Once you have a result you can call getPathname(), getRealPath(), getMTime(), and other standard methods — useful for cache invalidation or reporting.
Integrate with legacy Zend/Laminas module tooling carefully. If an existing application already depends on this package, isolate the call sites and document that the dependency is archived. Do not introduce it into new code solely for class discovery; Composer’s classmap generator or a short Symfony Finder + tokenizer script covers most modern use cases.
Prefer the underlying scanner for custom needs. If you need more than “which classes are in this file,” use Laminas\Code\Scanner\FileScanner directly rather than contorting the locator.
require, or a custom autoloader.getClasses() returns a collection; never assume one class per file or use the filename as the class name.class_alias are invisible. A class declared inside an if block or aliased dynamically will not be reliably discovered by static token scanning.token_get_all() or FileScanner directly. Also run composer why laminas/laminas-file to understand what is actually pulling the dependency in.ClassFileLocator and override its acceptance logic if you need custom file filtering. Keep such subclasses small and covered by tests, since upstream is frozen.classmap generation, symfony/finder plus token_get_all(), or roave/better-reflection is a better-supported choice.How can I help you explore Laravel packages today?