Kanishka Panamaldeniya
Kanishka Panamaldeniya

Reputation: 17566

Call static function with arguments , that has no arguments successfully execute

Hi I am working with forward_static_call_array() function , I got an example .

<?php

class A
{
    const NAME = 'A';
    public static function test() {
        $args = func_get_args();
        echo static::NAME, " ".join(',', $args)."<br/>";
    }
}

class B extends A
{
    const NAME = 'B';

    public static function test() {
        echo self::NAME, "<br/>";
        forward_static_call_array(array('A', 'test'), array('more', 'args'));
        forward_static_call_array( 'test', array('other', 'args'));
    }
}

B::test("bar");

function test() {
        $args = func_get_args();
        echo "C ".join(',', $args)."<br/>";
    }

?>

Here the result is

B
B more,args
C other,args

But when I looked at the code I saw that it is calling B::test("bar"); other 2 test() functions have func_get_args(). But B::test do not have it either. So I think it should pop up an error. But it is working fine .

How is that?

or

Please explain it . Thank you very much .

Upvotes: 0

Views: 372

Answers (1)

sectus
sectus

Reputation: 15454

Using forward_static_call_array is not special. Every function or method has this behavior.

PHP has support for variable-length argument lists in user-defined functions. This is implemented using the ... token in PHP 5.6 and later, and using the func_num_args(), func_get_arg(), and func_get_args() functions in PHP 5.5 and earlier.

function sum() {
    $acc = 0;
    foreach (func_get_args() as $n) {
        $acc += $n;
    }
    return $acc;
}

echo sum(1, 2, 3, 4);

http://php.net/manual/en/functions.arguments.php#functions.variable-arg-list

http://php.net/manual/en/functions.arguments.php#functions.variable-arg-list.old

Upvotes: 1

Related Questions