简体   繁体   中英

Use public functions inside a public function

Is it possible to use public functions inside a public function in php?

I got a few public functions which change the input and return it. I want to make a for statement inside a public function that loops through my functions, like:

$input

for= function1 -> output1 -> function2->output2->function3->output3.

I want to use the output of that for my next function. Also the 4 functions I have in my for loop has to loop 9 times.

in this case its about AES encrypt. i got 4 functions called: subBytes, shiftRows, mixColumns, addRoundkey.

This is my public function encrypt:

public function encrypt($input)
{
    $functions= ('subBytes', 'shiftRows', 'mixColumns', 'addRoundKey' );
    foreach($functions as $function)
    {
        $input = $$function($input);
    }

    return($input);
} //end function encrypt

and this is one of my functions:

public function subBytes($state)
{
    for ($row=0; $row<4; $row++){ // for all 16 bytes in the (4x4-byte) State
        for ($column=0; $column<4; $column++){ // for all 16 bytes in the (4x4-byte) State
            $_SESSION['debug'] .= "state[$row][$column]=" . $state[$row][$column] ."-->" . self::$sBox[$state[$row][$column]]."\n";
            $state[$row][$column] = self::$sBox[$state[$row][$column]];
        }
     }
     return $state;
}

Use code like this:

$output3 = function3(function2(function1($input)));

Or you can to add your function name into array and iterate over it:

$input = ''; // some value
$functioins = ('function1', 'function2', 'function3', 'function4');
foreach ($functions as $function) {
    $input = $$function($input);
}
$output = $input;

If we try to use public functions of object then:

public function encrypt($input)
{
    // array with methods names
    $methods= array('subBytes', 'shiftRows', 'mixColumns', 'addRoundKey' );
    foreach($methods as $method)
    {
        $input = $this->$method($input);
    }

    return($input);
}

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