PHP code example of samsonasik / is-deprecated

1. Go to this page and download the library: Download samsonasik/is-deprecated library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

samsonasik / is-deprecated example snippets


/**
 * @param  string|array $function the "functionName" or ["ClassName" or object, "functionName"] or "ClassName::functionName"
 * @throws InvalidArgumentException when trigger_error found but the error is not E_USER_DEPRECATED
 * @throws InvalidArgumentException when trigger_error and E_USER_DEPRECATED found but misplaced
 * @return bool
 */
function isDeprecatedUser($function): bool

/**
 * @param  callable $function callable function
 * @return bool
 */
function isDeprecatedWithActualCall(callable $function)

/**
 * @param  callable $function callable function
 * @return bool
 */
function isDeprecatedCore(callable $function): bool



use function IsDeprecated\isDeprecatedUser;
use function IsDeprecated\isDeprecatedWithActualCall;

function foo()
{
    trigger_error('this method has been deprecated.', E_USER_DEPRECATED);
    echo 'foo' . PHP_EOL;
}

function foonotdeprecated()
{
    echo 'foo' . PHP_EOL;
}

function fooDeprecatedWithCondition()
{
    if (1 === 1) {
        trigger_error('this method has been deprecated.', E_USER_DEPRECATED);
        echo 'foo' . PHP_EOL;
    }
}

// deprecated
var_dump(isDeprecatedUser('foo')); // true  OR for deprecate with condition
var_dump(isDeprecatedWithActualCall(function () {
    fooDeprecatedWithCondition();
})); // true

// not deprecated
var_dump(isDeprecatedUser('foonotdeprecated')); // false

// Usage Example:
if (isDeprecatedUser('foo')) {
    foonotdeprecated();;
} else {
    foo();
}



use function IsDeprecated\isDeprecatedUser;
use function IsDeprecated\isDeprecatedWithActualCall;

class Aclass
{
    function foo()
    {
        trigger_error('this method has been deprecated.', E_USER_DEPRECATED);
        echo 'foo' . PHP_EOL;
    }

    function foodeprecatedWithCondition()
    {
        if (1 === 1) {
            trigger_error('this method has been deprecated.', E_USER_DEPRECATED);
        }
        echo 'foo' . PHP_EOL;
    }

    function foonotdeprecated()
    {
        echo 'foo' . PHP_EOL;
    }

    // you may call inside the class
    // with $this as first index of array parameter
    function execute()
    {
        if (isDeprecatedUser([$this, 'foo'])) {
            $this->foonotdeprecated();
            return;
        }

        $this->foo();
    }
}

// deprecated
var_dump(isDeprecatedUser(['Aclass', 'foo'])); // true OR
var_dump(isDeprecatedUser([new \Aclass(), 'foo'])); // true OR
var_dump(isDeprecatedUser('Aclass::foo')); // true OR for deprecate with condition

var_dump(isDeprecatedWithActualCall(function () { // true
    new \Aclass()->foo();
}));

// not deprecated
var_dump(isDeprecatedUser(['Aclass', 'foonotdeprecated'])); // false OR
var_dump(isDeprecatedUser([new \Aclass, 'foonotdeprecated'])); // false OR
var_dump(isDeprecatedUser('Aclass::foonotdeprecated')); // false

// Usage Example:
if (isDeprecatedUser(['Aclass', 'foo'])) {
    (new \Aclass())->foonotdeprecated();;
} else {
    (new \Aclass())->foo();;
}



use function IsDeprecated\isDeprecatedCore;

$function = function () {
    mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
};

//on php 7.1
var_dump(isDeprecatedCore($function)); // true

//on php 7.0
var_dump(isDeprecatedCore($function)); // false

// Usage Example:
if (isDeprecatedCore($function)) {
    // alternative function, eg: openssl ...
} else {
    $function();
}