PHPackages                             jeremeamia/xstatic - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [PSR &amp; Standards](/categories/psr-standards)
4. /
5. jeremeamia/xstatic

Abandoned → [lhsazevedo/restatic](/?search=lhsazevedo%2Frestatic)Library[PSR &amp; Standards](/categories/psr-standards)

jeremeamia/xstatic
==================

Static interfaces without static pitfalls

1.0.1(12y ago)10321.7k↓26.7%6[2 issues](https://github.com/jeremeamia/xstatic/issues)1MITPHPPHP &gt;=5.3.3CI failing

Since Sep 25Pushed 5y ago9 watchersCompare

[ Source](https://github.com/jeremeamia/xstatic)[ Packagist](https://packagist.org/packages/jeremeamia/xstatic)[ RSS](/packages/jeremeamia-xstatic/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (2)Versions (6)Used By (1)

⛔ This is no longer supported
=============================

[](#no_entry-this-is-no-longer-supported)

Please consider using [ReStatic](https://github.com/lhsazevedo/restatic) instead.

XStatic
=======

[](#xstatic)

[![No Maintenance Intended](https://camo.githubusercontent.com/d904056147052e22d8e1c7f46bb50293ed2aeb4c43ead9a2d0cf7a48b46d0562/687474703a2f2f756e6d61696e7461696e65642e746563682f62616467652e737667)](http://unmaintained.tech/)

XStatic is a PHP library for enabling *static proxy interfaces*—similar to Laravel 4+ "Facades"—but with any PHP project. XStatic was created by [Jeremy Lindblom](https://twitter.com/jeremeamia).

***ATTENTION:** Please consider using [ReStatic](https://github.com/lhsazevedo/restatic), a maintained fork of this library. XStatic is no longer actively supported.*

### Introduction (Q&amp;A)

[](#introduction-qa)

> Facades? Static Proxies? Isn't using static methods considered a bad practice?

Using static methods and classes makes your code harder to test. This is because your code becomes tightly coupled to the class being referenced statically, and mocking static methods for unit tests is difficult. For this and other reasons, using static methods is generally discouraged by object-oriented programming (OOP) experts. Generally, techniques involving design patterns like *Service Locator* and *Dependency Injection* (DI) are preferred for managing object dependencies and composition.

> But... using static methods is really easy.

True, and PHP developers that prefer frameworks like CodeIgniter, Laravel, Kohana, and FuelPHP are very accustomed to using static methods in their application development. In some cases, it is an encouraged practice among these communities, who argue that it makes the code more readable and contributes to *Rapid Application Development* (RAD).

> So, is there any kind of compromise?

Yep! Laravel 4 has a concept called "facades" (Note: This is not the same as the [Facade design pattern](http://en.wikipedia.org/wiki/Facade_pattern)). These act as a static interface, or proxy, to an actual object instance stored in a service container. The static proxy is linked to the container using a few tricks, including defining class aliases via PHP's `class_alias()` function, and the use of the magic `__callStatic()` method. We can thank [Taylor Otwell](https://twitter.com/taylorotwell) for developing this technique.

> OK, then what is the point of XStatic?

XStatic uses the same technique as Laravel's "facades" system, but provides two additional, but important, features:

1. **It works with any framework's service container** - XStatic relies on the `ContainerInterface` of the [container-interop](https://github.com/container-interop/container-interop) project. You can use the [Acclimate library](https://github.com/jeremeamia/acclimate-container) to adapt any third-party containers to the normalized container interface that XStatic depends on.
2. **It works within any namespace** - XStatic injects an autoloader onto the stack, so no matter what namespace or scope you try to reference your aliased static proxy from, it will pass through the XStatic autoloader. You can configure XStatic to create the aliases in the global namespace, the current namespace, or a specific namespace.

> Oh, and why is it called XStatic?

Two reasons:

1. It **removes the static-ness** of making static method invocations, since the method calls are proxied to actual object instances. Potential tagline: *"Static interfaces without the static pitfalls"*.
2. It is pronounced like the word "ecstatic", because it is meant to provide developers (some of them at least) with a sense of joy.

Usage
-----

[](#usage)

To show you how to use XStatic, I will show you a simple [Silex](http://silex.sensiolabs.org/) application.

Your application bootstrap:

```
