PHPackages                             statical/statical - 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. statical/statical

ActiveLibrary

statical/statical
=================

Static proxy implementation

v1.1.0(11y ago)82.2k21MITPHPPHP &gt;=5.3.3

Since Nov 12Pushed 11y ago4 watchersCompare

[ Source](https://github.com/johnstevenson/statical)[ Packagist](https://packagist.org/packages/statical/statical)[ Docs](http://github.com/johnstevenson/statical)[ RSS](/packages/statical-statical/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (1)Versions (3)Used By (1)

Statical
========

[](#statical)

[![Scrutinizer Code Quality](https://camo.githubusercontent.com/bb2d0669cd14e163b64643f4e255f9c3fb18136bbe8b6f3c1190b71380375490/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a6f686e73746576656e736f6e2f737461746963616c2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/johnstevenson/statical/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/b156b27816724ccbc36411959d4556061d2d029b17d330203f7417752da65af7/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a6f686e73746576656e736f6e2f737461746963616c2f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/johnstevenson/statical/?branch=master)[![Build Status](https://camo.githubusercontent.com/2ba312382e04c09e1095751f0da73a70ef4ebb9e9d322f6d66e3f89064e216da/68747470733a2f2f7365637572652e7472617669732d63692e6f72672f6a6f686e73746576656e736f6e2f737461746963616c2e706e67)](http://travis-ci.org/johnstevenson/statical)

PHP static proxy library.

Contents
--------

[](#contents)

- [About](#About)
- [Usage](#Usage)
- [License](#License)

About
-----

[](#about)

**Statical** is a tiny PHP library that enables you to call class methods from a static accessor, so the static call to `Foo::doSomething()` actually invokes the `doSomething()` method of a specific class instance. To show a more concrete example:

```
# Normal access
$app->get('view')->render('mytemplate', $data);

# Using a static proxy
View::render('mytemplate', $data);
```

Both examples call the render method of the instantiated view class, with the static proxy version using terse and cleaner code. This may or may not be a good thing and depends entirely on your requirements, usage and point of view.

### How it Works

[](#how-it-works)

Everything runs through the `Statical\Manager`. It needs three pieces of data to create each static proxy:

- an alias *(which calls)*
- a proxy class *(which invokes the method in)*
- the target class

An **alias** is the short name you use for method calling: `Foo`, `View` or whatever.

You create a static **proxy class** like this. Note that its name is irrelevant and it is normally empty:

```
class FooProxy extends \Statical\BaseProxy {}

```

A **target class** is the class whose methods you wish to call. It can be either:

- an actual class instance
- a closure invoking a class instance
- a reference to something in a container or service-locator that resolves to a class instance.

This data is then registered using either the `addProxyInstance()` or the `addProxyService()` methods. See the [Usage](#Usage) section for some examples.

### Namespaces

[](#namespaces)

By default, each static proxy is registered in the global namespace. This means that any calls to `Foo` will not work in a namespace unless they are prefixed with a backslash `\Foo`. Alternatively you can include a *use* statement in each file: `use \Foo as Foo;`.

**Statical** includes a powerful namespacing feature which allows you to add namespace patterns for an alias. For example `addNamespaceGroup('path', Foo', 'App\\Library')` allows you to call `Foo` in any *App\\Library* or descendant namespace.

### Features

[](#features)

A few features in no particular order. Please see the [documentation](https://github.com/johnstevenson/statical/wiki/Home) for more information.

- **Statical** creates a static proxy to itself, aliased as *Statical* and available in any namespace, allowing you to call the Manager with `Statical::addProxyService()` or whatever. This feature can be disabled or modified as required.
- You can use any type of container/service-locator. If it implements `ArrayAccess` or has a `get` method then **Statical**will discover this automatically, otherwise you need to pass a callable as the target container.
- You can use multiple containers when adding proxy services to the Manager.
- If you pass a closure as a proxy instance, it will be invoked once to resolve the target instance. You can get a reference to this instance, or in fact any target class, by calling the *getInstance()* method on your alias, for example `Foo::getInstance()`.
- **Statical** is test-friendly. If you register a container then it is used to resolve the target instance for every proxied call, allowing you to swap in different objects. You can also replace a proxy by registering a different instance/container with the same alias.

Usage
-----

[](#usage)

Install via [composer](http://getcomposer.org)

```
composer require statical/statical

```

Below are some examples. Firstly, using a class instance:

```
