1

Good day,

I got the following issue. Have a method in a class that I want to call with array_walk with two arguments.

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

When I put a comment in the method SetAlias() it does respond. Hence it is being called.

The method being called is:

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

Arguments

But when I print the function arguments it returns :

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

Unfortunately this is not working for me.

So I COULD change the method's arguments to:

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

Still no results.

The code:

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

It currently returns:

parking_id, parking_country, parking_name

What am I missing?

Works by 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

Static context

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

1 回答 1

1

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, '`, `').'`');
    }
}
于 2015-07-20T08:00:42.760 回答