drpsrvu85668 2016-03-09 08:41
浏览 32

在公共函数中使用公共函数

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]]."
";
            $state[$row][$column] = self::$sBox[$state[$row][$column]];
        }
     }
     return $state;
}
  • 写回答

1条回答 默认 最新

  • dongming0505 2016-03-09 08:44
    关注

    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);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度