PHPackages                             brightnucleus/static-facade - 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. brightnucleus/static-facade

ActiveLibrary

brightnucleus/static-facade
===========================

Abstract static Facade class to wrap around shared object instances for convenient access.

v0.1.1(9y ago)111.3kMITPHP

Since Oct 25Pushed 9y ago1 watchersCompare

[ Source](https://github.com/brightnucleus/static-facade)[ Packagist](https://packagist.org/packages/brightnucleus/static-facade)[ RSS](/packages/brightnucleus-static-facade/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (2)Versions (3)Used By (0)

Bright Nucleus Static Facade
============================

[](#bright-nucleus-static-facade)

[![Latest Stable Version](https://camo.githubusercontent.com/47d6ddbbfdb7253fc66051d764f4db30e178fe3efa00244489801bb38fb13376/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6272696768746e75636c6575732f7374617469632d6661636164652e737667)](https://packagist.org/packages/brightnucleus/static-facade)[![Total Downloads](https://camo.githubusercontent.com/991ffc366969ab930e95fa52d355a1790764eaf5f399c97e8ca6ef77ada6cbd3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6272696768746e75636c6575732f7374617469632d6661636164652e737667)](https://packagist.org/packages/brightnucleus/static-facade)[![Latest Unstable Version](https://camo.githubusercontent.com/3a31257dcf767a330e3bf5a505476ee6a6af78efd2e6b0c723c857b913ac7ba1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f767072652f6272696768746e75636c6575732f7374617469632d6661636164652e737667)](https://packagist.org/packages/brightnucleus/static-facade)[![License](https://camo.githubusercontent.com/fbb5d733e3d482ef987df0a7fb71c636a345945a4f26859d2e36f97da2cd3c7b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6272696768746e75636c6575732f7374617469632d6661636164652e737667)](https://packagist.org/packages/brightnucleus/static-facade)

Abstract static Facade class to wrap around shared object instances for convenient access.

Table Of Contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
- [Basic Usage](#basic-usage)
    - [Extending The StaticFacade Class](#extending-the-staticfacade-class)
    - [Using The StaticFacadeTrait Trait](#using-the-staticfacadetrait-trait)
    - [Exception Thrown On Missing Method](#exception-thrown-on-missing-method)
    - [Adding Methods Directly To The Facade](#adding-methods-directly-to-the-facade)
- [Contributing](#contributing)
- [License](#license)

Installation
------------

[](#installation)

The best way to use this package is through Composer:

```
composer require brightnucleus/static-facade
```

Basic Usage
-----------

[](#basic-usage)

To create a static Facade, you can either extend the `StaticFacade` class or use the `StaticFacadeTrait`, depending on whether you need to extend an existing class or not.

Such a static Facade will then forward any static method calls it gets to the instance of the object it is wrapping as a dynamic method call.

As an example, let's say you have a `UserRepository` class to fetch users from. If you can't have that shared instance just be injected through the constructor (which would avoid a static coupling), you would normally either have a way of getting the shared instance through a static call (`UserRepository::getInstance()`) or have a Service Locator to get the shared instance (`Services::get('UserRepository')`). This produces cumbersome code, as every interaction with such a class consists of two separate steps.

To make this more convenient, you can provide a static Facade that abstracts away the fetching of the shared instance.

Example:

```
// Without a static Facade.
$userRepository = Services::get( 'UserRepository' );
$user = $userRepository->find( $userID );

// With a static Facade.
$user = UserRepository::find( $userID );
```

> Note: This creates a tight coupling within the consuming code to the static Facade itself. This is great for getting around limitations in legacy code, and is preferable to coupling your code to the actual class being used, as you have another layer abstraction and can modify/replace the actual class as needed. However, you should **always prefer having your dependencies be injected at runtime without creating such a tight coupling**.

### Extending The StaticFacade Class

[](#extending-the-staticfacade-class)

You would typically extend the `StaticFacade` class if you don't need to have your Facade extend another existing class.

Example:

```
