array_map回調函數內部變量做用域分享

<?php
class Test {
    // protected $num = 0; //test4
    // private $num = 0;   //test5
    // public $num = 0;    //test6
    
    public function __construct()
    {
        // $this->num = 0; //test3
    }
    
    public function fun()
    {
        $arr = array(
            array('people' => '修宇','hobby' => '拉拉拉'),
            array('people' => '棟浩','hobby' => '吼吼吼'),
            array('people' => '上線哲','hobby' => '哈哈哈'),
        );
        $people = '帥奇';
        $hobby = '喵喵喵';
        
        $num = 0;    //test1
        
        // $this->num = 0; //test2
        
        $arr = array_map(function($v) use($people, $hobby){
            $num++; //test1
            // $this->num++;    //test2345
            $v['people'] = $people.$num;
            $v['hobby'] = $hobby.$num;   //test1   
            
            // $v['people'] = $people.$this->num;
            // $v['hobby'] = $hobby.$this->num;    //test2 or test3 or test4 or test5 or test6
            return $v;
        },$arr);
        // return $arr;
        
        foreach($arr as $v) {
            $num++;
            $v['people'] = $people.$num;
            $v['hobby'] = $hobby.$num;
        }
        return $arr;
    }
}
$test = new Test();
var_dump($test->fun());
?>

如上code 目的爲在回調用使變量自增 , 經測除fun函數內部變量只被++一次 其他類的屬性均無此做用域問題 !
foreach 無該做用域問題 !
想來原理很簡單 這裏array_map用的是匿名回調函數 . 回調函數的層級本就與fun方法應相同 , 只不過匿名閉包使回調函數寫在array_map中.
$num變量爲fun函數內部變量 , 其做用域再fun函數內. 綜上所述回調函數與fun函數同級. 故$num並不做用在回調函數內. 故沒法自增.
同理類的屬性做用域即在fun函數也在回調函數 !php

回調非匿名使用場景原理以下 :閉包

<?php
class Test {
    // protected $num = 0; //test4
    // private $num = 0;   //test5
    // public $num = 0;    //test6
    
    public function __construct()
    {
        $this->num = 0; //test3
    }
    
    public function callBack($v, $rV) {
        $this->num++;
        $v['people'] = $rV['people'].$this->num;
        $v['hobby'] = $rV['hobby'].$this->num;
        return $v;
    }
    
    public function fun()
    {
        $arr = array(
            array('people' => '修宇','hobby' => '拉拉拉'),
            array('people' => '棟浩','hobby' => '吼吼吼'),
            array('people' => '上線哲','hobby' => '哈哈哈'),
        );
    
        $num = 0;    //test1
       
        $replace = array(
            array('people' => '帥奇', 'hobby' => '喵喵喵'),
            array('people' => '帥奇', 'hobby' => '喵喵喵'),
            array('people' => '帥奇', 'hobby' => '喵喵喵'),
        );
        $arr = array_map(array($this,'callBack'),$arr, $replace);
        return $arr;
    }
}
$test = new Test();
var_dump($test->fun());
?>

上述array_map函數想傳計數器都很差傳 , 因而使用array_walk
array_walk 類的屬性函數

<?php
class Test {
    // protected $num = 0; //test4
    private $num = 0;   //test5
    // public $num = 0;    //test6
    
    public function __construct()
    {
        // $this->num = 0; //test3
    }
    
    public function callBack($v, $k) {
        $this->num++;
        var_dump($this->num.':'.$k.'=>'.$v);
    }
    
    public function fun()
    {
        $arr = array('people' => '修宇','hobby' => '拉拉拉');
        array_walk($arr, array($this,'callBack'));
    }
}
$test = new Test();
$test->fun();
?>

array_walk fun函數內的局部變量 :this

<?php
class Test {
    public function callBack($v, $k, $num) {
        $num++;
        var_dump($num.':'.$k.'=>'.$v);
    }
    
    public function fun()
    {
        $arr = array('people' => '修宇','hobby' => '拉拉拉');
        
        $num = 0;
        
        array_walk($arr, array($this, 'callBack'), $num);
    }
}
$test = new Test();
$test->fun();
?>

由array_walk fun函數內的局部變量狀況 就引起array_walk、和array_map底層實現的探究
※不少帖子說array_walk與foreach同樣 , 這麼看不同 . 使用時要注意 !code

相關文章
相關標籤/搜索