bugo/smf-compat

Wrappers to work with global variables and deprecated functions in SMF

0.2.3 2024-11-08 06:04 UTC

This package is auto-updated.

Last update: 2024-11-12 08:00:48 UTC


README

SMF 2.1 PHP Coverage

По-русски

Description

The package is designed to prepare current SMF 2.1 modifications for the future migration to 3.0.

The proposed utility classes eliminate the need to declare global variables in your modification code.

As a result, your modifications will be able to work in both SMF 2.1 and 3.0, with minimal changes.

Installation

In the root directory of your modification, run the command:

composer require bugo/smf-compat

Then in app.php (or other similar entry point), inlcude autoload.php:

require_once __DIR__ . '/vendor/autoload.php';

Usage

Legacy code

<?php

class Example
{
    public function method1()
    {
        global $txt;

        echo $txt['hello_world'];
    }

    public function method2()
    {
        global $user_info, $modSettings;

        echo $user_info['name'];

        var_dump($modSettings);
    }
}

New code

<?php

use Bugo\Compat\Lang;
use Bugo\Compat\User;
use Bugo\Compat\Config;

class Example
{
    public function method1()
    {
        echo Lang::$txt['hello_world'];
    }

    public function method2()
    {
        echo User::$info['name'];

        var_dump(Config::$modSettings);
    }
}

After upgrading to SMF 3.0, it will be enough to replace the used classes:

-use Bugo\Compat\Lang;
+use SMF\Lang;
-use Bugo\Compat\User;
+use SMF\User;
-use Bugo\Compat\Config;
+use SMF\Config;

Or you can leave it as it is. In this case, your modification will support both versions of SMF.

List of suggested replacements

Global variables

Functions

SSI functions

All functions in SSI.php that were called via ssi_function_name before 3.0 are called this way in 3.0: ServerSideIncludes::function_name.

Working with the database

In compatibility mode, you can use Utils::$smcFunc['db_query'], etc., but also introduced a new class Db, with a static property $db containing the class of the current database engine. The methods of this class are similar to functions in Utils::$smcFunc, but without the db_ prefix. Let's show on the example of three popular functions:

Examples of using this library