简体   繁体   中英

Small PHP error Too few arguments to function on php72

I need help fixing a small problem with my php script to work on php7.2, the code below works on php7.0 but doesn't work on php7.1 or 7.2 :(

Error showing on debug:

    ArgumentCountError: Too few arguments to function smarty_function_gravatar(), 1 passed and exactly 2 expected in public_html/includes/hooks/custom-function.php: Line 91


Stack trace:
    #0 /public_html/includes/hookfunctions.php(0): smarty_function_gravatar(Array)
    #1 /public_html/includes/clientareafunctions.php(0): run_hook('ClientAreaPage', Array)
    #2 /public_html/login.php(0): outputClientArea('login', false, Array)
    #3 /public_html/member/viewticket.php(0): unknown()
    #4 {main}

My original code on line 91:

 function smarty_function_gravatar($params, &$smarty) {
    $email = (isset($params['email']) ? trim(strtolower($params['email'])) : '');
    $rating = (isset($params['rating']) ? $params['rating'] : 'R');
    $url = "https://www.gravatar.com/avatar/".md5($email) . "?r=".$rating;

    if(isset($params['default']))
        $url .= "&d=".urlencode($params['default']);
    if(isset($params['size']))
        $url .= "&s=".$params['size'];

    if(isset($params['assign'])) {
        $smarty->assign($params['assign'], $url);
        return;
    }
    return $url;
 }

On template .tpl

<img src="{gravatar email="{if $reply.name eq 'Admin 1'}admin1@domain.com{elseif $reply.name eq 'Admin 2'}admin2@domain.com{elseif $reply.name eq 'Admin 3'}admin3@domain.com{elseif $reply.name eq 'Admin 4'}admin4@domain.com{/if}" size="140"}" height="60" width="60">

{elseif $reply.contactid} <img src="{gravatar email="$replyemail" size="140"}" height="60" width="60">

{elseif $reply.userid} 
<img src="{gravatar email="$replyemail" size="140" default="/default-avatar.png"}" height="60" width="60">
{else} 
<img src="{gravatar email="$replyemail" size="140" default="/default-avatar.png"}" height="60" width="60">

Missing arguments would only trigger a warning until PHP/7.0 but since PHP/7.1 they throw a fatal error ( demo ). From 7.0 migration guide :

Previously, a warning would be emitted for invoking user-defined functions with too few arguments. Now, this warning has been promoted to an Error exception. This change only applies to user-defined functions, not internal functions.

Chances are that it never really worked but you've configured PHP to hide error information—something that's no longer useful with fatal errors because they abort execution anyway.

Since you never use the function's second argument, just get rid of it entirely.

see kevinpapst.de (in German); it's from 2008 and so is the syntax ...

meanwhile the documentation says something else about "Writing Plugins":

As a general rule, the currently evaluated template's Smarty_Internal_Template object is always passed to the plugins as the last parameter with two exceptions:

  • modifiers do not get passed the Smarty_Internal_Template object at all.

  • blocks get passed $repeat after the Smarty_Internal_Template object to keep backwards compatibility to older versions of Smarty.

for example:

function smarty_function_gravatar(array $params, Smarty_Internal_Template $template) {

    $email = (isset($params['email']) ? trim(strtolower($params['email'])) : '');
    $rating = (isset($params['rating']) ? $params['rating'] : 'R');

    $url = "https://www.gravatar.com/avatar/".md5($email) . "?r=".$rating;
    if(isset($params['default'])) {
        $url .= "&d=".urlencode($params['default']);
    }
    if(isset($params['size'])) {
        $url .= "&s=".$params['size'];
    }
    if(isset($params['assign'])) {
        $template->smarty->assign($params['assign'], $url);
        return;
    }
    return $url;
}

to be used alike:

{gravatar email="example@example.com" size="60" rating="X" assign="gravatarURL" default="http://www.example.com/default_gravatar.jpg"}

<img src="{$gravatarURL}" height="60" width="60">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM