setting scope of array_map php

前端 未结 3 1203
情话喂你
情话喂你 2021-01-14 16:31

hey all, i use array_map from time to time to write recursive methods. for example

function stripSlashesRecursive( $value ){

    $value = is_array($value) ?         


        
3条回答
  •  自闭症患者
    2021-01-14 17:00

    When using a class method as a callback for functions like array_map() and usort(), you have to send the callback as two-value array. The 2nd value is always the name of the method as a string. The 1st value is the context (class name or object)

    // Static outside of class context
    array_map( array( 'ClassName', 'methodName' ), $array );
    
    // Static inside class context
    array_map( array( __CLASS__, 'methodName' ), $array );
    
    // Non-static outside of object context
    array_map( array( $object, 'methodName' ), $array );
    
    // Non-static inside of object context
    array_map( array( $this, 'methodName' ), $array );
    

提交回复
热议问题