簡體   English   中英

類中具有多個參數的PHP array_walk()

[英]PHP array_walk() in class with multiple arguments

美好的一天,

我遇到了以下問題。 我要在帶有兩個參數的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);
}

參數

但是當我打印函數參數時,它返回:

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');
?>

當前返回:

parking_idparking_countryparking_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, '`, `').'`');
}

但是不能在靜態上下文中工作。 有可能使這項工作嗎?

 $fn = $fn->BindTo(__CLASS__); $SetFields = array_map( $fn, $SetFields ); 
  • 警告:Closure :: bindTo()期望參數1為對象,給定字符串

靜態環境

<?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');
?>

作品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返回一個布爾值,並且它不會改變輸入數組,因此您不會以這種方式將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, '`, `').'`');
    }
}

編輯:對於您的靜態類:

    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, '`, `').'`');
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM