Search code examples
phpclassarray-walk

PHP array_walk() in class with multiple arguments


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

Solution

  • array_walk returns a boolean and it doesn't alter the input array, so you won't get the values from SetAlias() back into GetFields() this way.
    But you can use aray_map() instead.

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

    edit: For your static class:

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

    (are you sure having everything static makes sense in your design?)

    edit: or another one.....

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