来自数组

时间:2017-12-13 16:45:26

标签: php arrays function

嗨我想从数组中获取函数名称 该怎么做

public function cuturls($url){
    $long_url = urlencode($url);
    $api_token = 'xxx';
    $api_url = "https://cut-urls.com/api?api={$api_token}&url={$long_url}";
    $result = @json_decode(file_get_contents($api_url),TRUE);
    if($result["shortenedUrl"]) {
        return $result["shortenedUrl"];
    } else {
        return $this->ouo($url);
    }
}

想要转换此功能

public function docut($url){
    global $filesize77;
    global $cutpost;
    if(empty($cutpost) or $cutpost==='cutearn'){
        if($filesize77 <= '1073741824'){
            return array($this->cuturls($url),'cuturls');
        }else if($filesize77 > '1073741824' and $filesize77 <= '2147483648'){
            return array($this->cuturls($this->adlinkme($url)),'cuturls');
        }else if($filesize77 > '2147483648' and $filesize77 <= '3221225472'){
            return array($this->cuturls($this->adlinkme($this->cutearn($url))),'cuturls');
        }
    }

希望通过使用数组转换为此类型具有函数名称 如何回调

public function docut($url){
    global $filesize77;
    global $cutpost;
    $cutarray = array('cuturls', 'adlinkme', 'cutearn', 'cutwin');
    if(empty($cutpost) or $cutpost===$cutarray[3]){
        if($filesize77 <= '1073741824'){
            return array($this->$cutarray[0]($url),$cutarray[0]);
        }else if($filesize77 > '1073741824' and $filesize77 <= '2147483648'){
            return array($this->$cutarray[0]($this->$cutarray[1]($url)),$cutarray[0]);
        }else if($filesize77 > '2147483648' and $filesize77 <= '3221225472'){
            return array($this->$cutarray[0]($this->$cutarray[1]($this->$cutarray[2]($url))),$cutarray[0]);
        }
    }

2 个答案:

答案 0 :(得分:0)

call_user_func_array($func,$parameters);

$func是函数名称作为字符串,$parameters是作为数组的$func的参数。 阅读文档: - http://php.net/manual/en/function.call-user-func-array.php

用法: -

<?php
function abc($str){
    print $str;
}
call_user_func_array("abc",array('Working'));
?>

当它提出你的问题时: -

public function docut($url){
    global $filesize77;
    global $cutpost;
    $cutarray = array('cuturls', 'adlinkme', 'cutearn', 'cutwin');
    if(empty($cutpost) or $cutpost===$cutarray[3]){
        if($filesize77 <= '1073741824'){
            return array(call_user_func_array(array($this,$cutarray[0]),$url),$cutarray[0]);
        }else if($filesize77 > '1073741824' and $filesize77 <= '2147483648'){
            return array( call_user_func_array(array($this,$cutarray[0]),call_user_func_array(array($this,$cutarray[0]),$url)),$cutarray[0]);
        }else if($filesize77 > '2147483648' and $filesize77 <= '3221225472'){
            return array(call_user_func_array(array($this,$cutarray[0]),call_user_func_array(array($this,$cutarray[1]),call_user_func_array(array($this,$cutarray[2]),$url))),$cutarray[0]);
        }
    }
}

答案 1 :(得分:0)

只需使用{..}(..)语法即可。 I.e $this->{$cutarray[0]}(...)

public function docut($url){
    global $filesize77;
    global $cutpost;
    $cutarray = ['cuturls', 'adlinkme', 'cutearn', 'cutwin'];
    if(empty($cutpost) or $cutpost===$cutarray[3]){
        if($filesize77 <= '1073741824') {
            return [$this->{$cutarray[0]}($url), $cutarray[0]];
        } else if($filesize77 > '1073741824' and $filesize77 <= '2147483648'){
            return [$this->{$cutarray[0]}($this->$cutarray[1]($url)), $cutarray[0]];
        } else if($filesize77 > '2147483648' and $filesize77 <= '3221225472') {
            return [$this->{$cutarray[0]}($this->{$cutarray[1]}($this->{$cutarray[2]}($url))), $cutarray[0]];
        }
    }

如果你向这个函数添加更多的情况,你应该考虑将它包装在循环或其他内容中。目前您的代码很难阅读和理解......

public function docut($url){
    global $filesize77;
    global $cutpost;
    $cutarray = ['cuturls', 'adlinkme', 'cutearn', 'cutwin'];
    if(empty($cutpost) or $cutpost === $cutarray[3]) {
        switch {
        case: $filesize77 > 0x80000000 && $filesize77 <= 0xC0000000:
            $url = $this->{$cutarray[2]}($url);
        case: $filesize77 > 0x40000000 && $filesize77 <= 0x80000000:
            $url = $this->{$cutarray[1]}($url);
        case $filesize77 < 0x40000000;
            $url = $this->{$cutarray[0]}($url);
        }

        return [$url, $cutarray[0]];
    }
}