abstract class \mpf\base\App

abstract class \mpf\base\App

App class is extended, by default, by \mpf\WebApp and \mpf\ConsoleApp and those are the main classes used by the framework to run a website or a terminal application. It can also be extended by a custom class if that's needed to create a specific type of application. Any class that extends this must include ::start() method that will be called from run() method.

It is usually instantiated in the index file of the application like this:

Framework App index file

   'startTime' => microtime(true),
   'autoload' => $autoload // <- an instance of composer autoload class;

Some classes from the framework also require that 2 constants should be defined in the index before running the main app: LIBS_FOLDER and APP_ROOT.

-LIBS_FOLDER records the location of "vendor" folder created by composer

-APP_ROOT records the location of "php" folder of the main app, where all php files are found.

Those are used by autoloader to load all classes when needed and find other folders locations.

Index file should also define an error handler. All exceptions are already handled by the app automatically by for errors the programmer must handle them.

This can be done by something similar to this:

set_error_handler(function ($errno, $errstr, $errfile, $errline) {
    $severity = 1 * E_ERROR | // change 0 / 1 value to ignore / handle different errors;
        1 * E_WARNING |
        1 * E_PARSE |
        1 * E_NOTICE |
        0 * E_CORE_ERROR |
        0 * E_CORE_WARNING |
        0 * E_COMPILE_ERROR |
        0 * E_COMPILE_WARNING |
        1 * E_USER_ERROR |
        1 * E_USER_WARNING |
        1 * E_USER_NOTICE |
        0 * E_STRICT |
        0 * E_DEPRECATED |
        0 * E_USER_DEPRECATED;
    $ex = new \ErrorException($errstr, 0, $errno, $errfile, $errline);
    if (($ex->getSeverity() & $severity) != 0) {
        \mpf\ConsoleApp::get()->error($errstr, array(
            'File' => $errfile,
            'Line' => $errline,
            'Number' => $errno,
            'Trace' => $ex->getTraceAsString()

How to access App class

App class can be accessed from anyplace by calling the exact type of app used or by calling the general app. In models and classes that are used by multiple types of apps is better to access the main app. It will return the current instance created in index. For the example written above the following code will return a \mpf\ConsoleApp instance: $inst = \mpf\base\App::get().

Accessing components from the app is really ways because of this:

// access Sql datasource and run a query:
\mpf\base\App::get()->sql()->Query("SELECT * FROM `something`");
// access redis datasource and read a key:

public int $startTime

Set from index to measure total time. To be sent as config option in for ::run() method. It can also be set in config of the app as that file should be loaded at the start also.

public string $title = "MPF App"

Full title of the current application. For web app it will be used, by default, as the title of the website in the <title></title> tag. It will aslo be used by email classes as prefix of the subject.

public string $shortName = "app"

To be used by common keys like Redis, Cookies, Session, Files Path, any other Cache system and so on.. anything that can be accessed by multiple websites.

public string $cacheEngineClass

Class name for cache engine. Must have implement CacheInterface. If none is sent then no cache will be used.

protected \Composer\Autoload\ClassLoader $autoload

Link to composer autoload class. To be sent from index file when running the app. It will be used internally to get the location of some modules and classes.

private static \mpf\base\App $_instance = [mpf\ConsoleApp]

Records current app instance; Can't be accessed directly but can be accessed by calling \mpf\base\App::get() method.


public static \mpf\base\App run (string[] $config = [])

  • $config

    -no description found-

Starts a new application using selected configuration; This method should be called from the index file like this:

   'startTime' => microtime(true),
   'autoload' => $autoload // <- an instance of composer autoload class;

\mpf\ConsoleApp should be replaced with the app that should be run at that time.

This method wil handle all Exceptions and then will call the ::start() method defined by the instantiated child class and return the instance of the object.

public static \mpf\base\App get ()

Fast access to active App child instance; Can be called from any place to get fast access to any components.


  \mpf\base\App::get()->sql(); // get access to \mpf\datasources\sql\PDOConnection

protected abstract mixed start ()

This method is to be executed when the new application it's instantiated. It can have code that needs to be executed in the beginning for the specific application type;

public \mpf\datasources\sql\PDOConnection sql (string[] $options = [])

  • $options

    Optional; Some config options can be set and it will return a specific instance with that config applied.

Shortcut to Sql database connection;

public \Predis\Client redis (string[] $options = [])

  • $options

    Optional; Some config options can be set and it will return a specific instance with that config applied.

Shortcut to Redis database connection

public mixed cacheValue (mixed $key)

  • $key

    -no description found-

Get value for selected cache key. If cache engine is not set it will return null.

public mixed cacheExists (string $key)

  • $key

    -no description found-

Check if selected key is saved in cache. If no cache engine is set it will return false.

public null cacheSet (string $key, mixed $value)

  • $key

    -no description found-

  • $value

    -no description found-

Set a value for selected key in the selected cache object.

public \mpf\interfaces\CacheInterface cache ()

Get an instance of selected Cache object or null if none is set;

public \Composer\Autoload\ClassLoader autoload ()

Link to composer autoload class