Home

Awesome

Statical

Scrutinizer Code Quality Code Coverage Build Status

PHP static proxy library.

Contents

<a name="About"></a>

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

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

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:

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

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

A few features in no particular order. Please see the documentation for more information.

<a name="Usage"></a>

Usage

Install via composer

composer require statical/statical

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

<?php
$alias = 'Foo';
$proxy = 'Name\\Space\\FooInstance';
$instance = new FooClass();

# Create our Manager
$manager = new Statical\Manager();

# Add proxy instance
$manager->addProxyInstance($alias, $proxy, $instance);

# Now we can call FooClass methods via the static alias Foo
Foo::doSomething();

For a container or service-locator you would do the following:

<?php
$alias = 'Foo';
$proxy = 'Name\\Space\\FooService';

# FooService id in container
$id = 'bar';

# Add it to the container
$container->set($id, function ($c) {
  return new FooService($c);
});

# Create our Manager
$manager = new Statical\Manager();

# Add proxy service
$manager->addProxyService($alias, $proxy, $container, $id);

# FooService is resolved from the container each time Foo is called
Foo::doSomething();

If the container id is a lower-cased version of the alias, then you can omit the $id param. Using the above example:

<?php
$alias = 'Foo';
...
# FooService id in container
$id = 'foo';
...

# Add proxy service - note we don't need to pass the id
$manager->addProxyService($alias, $proxy, $container);
...

In the above examples, the service is resolved out of the container automatically. If the container doesn't implement ArrayAccess or doesn't have a get method, you need to pass a callable:

<?php
$alias = 'Foo';
$proxy = 'Name\\Space\\FooService';

# Our container uses magic calls
$di->foo = new FooService();

# so we need to pass a callable as the container param
$container = array($di, '__get');
...
# Add proxy service
$manager->addProxyService($alias, $proxy, $container);
...

Full usage documentation can be found in the Wiki.

<a name="License"></a>

License

Statical is licensed under the MIT License - see the LICENSE file for details