PHP array_walk() in class 有多个参数

PHP array_walk() in class with multiple arguments

美好的一天,

我遇到了以下问题。 class 中有一个方法,我想用 array_walk 调用,有两个参数。

array_walk($fields, array($this, 'SetAlias'), $Table);

当我在方法 SetAlias() 中添加注释时,它确实有响应。因此被称为。

正在调用的方法是:

private function SetAlias($value, $table){
    if(isset($this->alias[$table][$value]) === true){
            $value = $value.'` AS `'.$this->alias[$table][$value];
    }
    return($value);
}

参数

但是当我打印函数参数时 returns :

Array
    (
        [0] => parking_id
        [1] => 0
        [2] => parking
    )

不幸的是,这对我不起作用。

所以我可以将方法的参数更改为:

private function SetAlias($value, $null, $table){

仍然没有结果。

代码:

<?php

class Test {
    public  $fields                     =   array('parking' => array('parking_id', 'parking_country', 'parking_name'));
    public  $alias                          =   array();



    private function SetAlias($value, $table){
        if(isset($this->alias[$table][$value]) === true){
                $value = $value.'` AS `'.$this->alias[$table][$value];
        }
        return($value);
    }


    public function GetFields($Table){
        $fields = $this->fields[$Table];

        if(isset($this->alias[$Table]) === true){
            array_walk($fields, array($this, 'SetAlias'), $Table);
        }

        return('`'.implode($fields, '`, `').'`');
    }

}





$example = new Test();
$example->alias['parking'] = array('parking_id'=>'id', 'parking_country'=>'country');

echo $example->GetFields('parking');
?>

目前returns:

parking_id, parking_country, parking_name

我错过了什么?

VolkerK作品

public function GetFields($table){
    $fields = $this->fields[$table];

    if(isset($this->alias[$table]) === true) {
        $fn = function($e) use ($table) {
            return $this->SetAlias($e, $table);
        };
        $fn = $fn->BindTo($this);
        $fields = array_map( $fn, $fields );
    }

    return('`'.implode($fields, '`, `').'`');
}

Does not work in a static context though. Is it possible to make this work?

$fn = $fn->BindTo(__CLASS__);
$SetFields = array_map( $fn, $SetFields );
  • Warning: Closure::bindTo() expects parameter 1 to be object, string given

静态上下文

<?php

class Test {
    public static $fields                           =   array('parking' => array('parking_id', 'parking_country', 'parking_name'));
    public static $alias                            =   array();

    private static function SetAlias($value, $table){
        if(isset(self::$alias[$table][$value]) === true){
            $value = $value.'` AS `'.self::$alias[$table][$value];
        }
        return($value);
    }


    public static function GetFields($table){
        $fields = self::$fields[$table];

        if(isset(self::$alias[$table]) === true) {
            $fn = function($e) use ($table) {
                return self::SetAlias($e, $table);
            };
            $fn = $fn->BindTo(__CLASS__);
            $fields = array_map( $fn, $fields );
        }

        return('`'.implode($fields, '`, `').'`');
    }
}





Test::$alias['parking'] = array('parking_id'=>'id', 'parking_country'=>'country');

echo Test::GetFields('parking');
?>

WORKS VolkerK:

 <?php
class Test {
    public static $fields   = array('parking' => array('parking_id', 'parking_country', 'parking_name'));
    public static $alias    = array();

    private static function SetAlias($value, $table){
        if(isset($table[$value]) === true){
            $value = $value.'` AS `'.$table[$value];
        }
        return($value);
    }

    protected static function getFieldMapper($table) {
        if( !isset(self::$alias[$table]) ) {
            return function($e) { return $e; };
        }

        $table = self::$alias[$table];
        return function($e) use ($table) {
            return Test::SetAlias($e, $table);
        };
    }

    public static function GetFields($table){
        $fields = array_map( self::getFieldMapper($table), self::$fields[$table]);
        return('`'.implode($fields, '`, `').'`');
    }
}
?>

array_walk returns 一个布尔值,它不会改变输入数组,因此您不会通过这种方式将 SetAlias() 的值返回到 GetFields() 中。
但是您可以改用 aray_map()

<?php
class Test {
    public  $fields = array('parking' => array('parking_id', 'parking_country', 'parking_name'));
    public  $alias  = array();

    private function SetAlias($value, $table) {
        if(isset($this->alias[$table][$value]) === true){
            $value = $value.'` AS `'.$this->alias[$table][$value];
        }
        return($value);
    }


    public function GetFields($table){
        $fields = $this->fields[$table];

        if(isset($this->alias[$table]) === true) {
            $fn = function($e) use ($table) {
                return $this->SetAlias($e, $table);
            };
            $fn = $fn->BindTo($this);
            $fields = array_map( $fn, $fields );
        }

        return('`'.implode($fields, '`, `').'`');
    }
}

编辑:对于您的静态 class:

    if(isset(self::$alias[$table]) === true) {
        $fn = function($e) use ($table) {
            return Test::SetAlias($e, $table);
        };
        $fields = array_map( $fn, $fields );
    }

(您确定在您的设计中让所有静态内容都有意义吗?)

编辑:或另一个.....

class Test {
    public static $fields   = array('parking' => array('parking_id', 'parking_country', 'parking_name'));
    public static $alias    = array();

    private static function SetAlias($value, $table){
        if(isset($table[$value]) === true){
            $value = $value.'` AS `'.$table[$value];
        }
        return($value);
    }

    protected static function getFieldMapper($table) {
        if( !isset(self::$alias[$table]) ) {
            return function($e) { return $e; };
        }

        $table = self::$alias[$table];
        return function($e) use ($table) {
            return Test::SetAlias($e, $table);
        };
    }

    public static function GetFields($table){
        $fields = array_map( self::getFieldMapper($table), self::$fields[$table]);
        return('`'.implode($fields, '`, `').'`');
    }
}