friendsofphp/proxy-manager-lts
Long-term support fork of ProxyManager for PHP. Generates lazy-loading, access-interceptor, and value-holder proxies for dependency injection, caching, and AOP-style patterns. Works with modern PHP and frameworks to optimize object creation and performance.
Start by requiring the package via Composer:
composer require friendsofphp/proxy-manager-lts
No framework integration is required out-of-the-box — it’s framework-agnostic. Your first use case is likely implementing lazy loading for heavy dependencies. For example, to lazily instantiate a costly service (e.g., database-dependent client), create a proxy factory and generate a proxy class:
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
use ProxyManager\Proxy\LazyLoadingInterface;
$factory = new LazyLoadingValueHolderFactory();
$proxy = $factory->createProxy(
HeavyService::class,
function (&$wrappedObject, $proxy, $method, $params, &$initializer) {
$initializer = null; // Initialize only once
$wrappedObject = new HeavyService(); // Actual instantiation
return true;
}
);
// HeavyService is *not* instantiated until a method is called
$proxy->doSomethingExpensive(); // Only now is HeavyService constructed
Check the examples/ directory in the source repo (if available) or the original ProxyManager docs for foundational concepts — this LTS fork maintains the same API surface.
lazy tagged services.use ProxyManager\Factory\AccessInterceptorScopeLocalizerFactory;
use ProxyManager\Proxy\AccessInterceptorInterface;
$factory = new AccessInterceptorScopeLocalizerFactory();
$proxy = $factory->createProxy(
Service::class,
['preSetMethod' => function ($instance, $method, &$params) {
logger()->debug("Calling $method");
}]
);
User->getProfile()) to avoid N+1 queries. The proxy defers loading until getProfile() is accessed.ProxyManager\Configuration::setProxyGenerator() with a custom generator that writes to var/proxies/. Reduces runtime overhead significantly.__call() in your base classes, as proxies often rely on __construct() and signature matching.ScopeLocalizer to scope interceptors to request context.Reflection, so mismatches break silently if not tested.ProxyManager\Util\ProxyGenerator::generate() and ClassLoader::addClassLoader(). Avoid runtime compilation in prod.How can I help you explore Laravel packages today?