2012-08-02 3 views
1

getContent() 함수에 대한 호출이 여러 개 있는데, 인수는 func_get_args()을 사용하여 함수로 가져 왔습니다. 인수 중 일부는 동일하게 유지되며 이러한 함수 호출 전체에서 반복되는 인수를 복제하지 않아도되도록 변수로 설정하려고합니다.PHP 함수 인수를 문자열로 결합

<? $myStyle = 
    "show:<div class='box box_stories'>", 
    "show:<div class='box_pad box_img bwWrapper'>", 
    "show:<div class='box_inner_pad'>", 
    "show:<h3><a href='/staff-detail/__slug__'>__authorX__</a></h3>", 
    "show:<p class='teamsummary'>__summary__</p>", 
    "show:<p class='phone_icon'>__authorXworkphone__</p>", 
    "show:</div>", 
    "show:</div>", 
    "show:</div>"; 

getContent(
    "article", 
    "display:list", 
    "find_series:team", 
    "find_category:leadership-team", 
    "order:series", 
    "find_parent_category:team", 
    $myStyle 
); 

getContent(
    "article", 
    "display:list", 
    "find_series:team", 
    "find_category:executive-team", 
    "order:series", 
    "find_parent_category:team", 
    $myStyle 
); ?> 

가 어떻게이게 가능 : 나는 이런 식으로 뭔가를 할 수있을 것

<? getContent(
    "article", 
    "display:list", 
    "find_series:team", 
    "find_category:pastors-team", 
    "order:series", 
    "find_parent_category:team", 
    "show:<div class='box box_stories'>", 
    "show:<div class='box_pad box_img bwWrapper'>", 
    "show:<div class='box_inner_pad'>", 
    "show:<h3><a href='/staff-detail/__slug__'>__authorX__</a></h3>", 
    "show:<p class='teamsummary'>__summary__</p>", 
    "show:<p class='phone_icon'>__authorXworkphone__</p>", 
    "show:</div>", 
    "show:</div>", 
    "show:</div>" 
); 
?> 

이상적 : 여기

전체 인수를 사용하여 함수 호출의 예입니다? 이것을 접근하는 가장 좋은 방법은 무엇입니까?

참고 : 나는의 getContent() 함수를 변경할 수 없습니다, 그것은 불행하게도, 이것이 가능하지 않은 CMS

답변

1

이 내가 생각할 수있는 가장 간단한 하나입니다

call_user_func_array('getContent',array_merge(array(
    "article", 
    "display:list", 
    "find_series:team", 
    "find_category:executive-team", 
    "order:series", 
    "find_parent_category:team", 
    ),$myStyle) 
); 

편집 : Yazmat의 솔루션은 아마 더 좋을 것이다.
이 같은 가변 수의 인수로 사용할 수 있습니다 :

<?php 
function myGetContent() { 
    $defaults = array(
     "show:<div class='box box_stories'>", 
     "show:<div class='box_pad box_img bwWrapper'>", 
     "show:<div class='box_inner_pad'>", 
     "show:<h3><a href='/staff-detail/__slug__'>__authorX__</a></h3>", 
     "show:<p class='teamsummary'>__summary__</p>", 
     "show:<p class='phone_icon'>__authorXworkphone__</p>", 
     "show:</div>", 
     "show:</div>", 
     "show:</div>" 
    ); 
    return call_user_func_array('getContent',array_merge(func_get_args(), $defaults)); 
} 
+0

불행히도 이것도 작동하지 않습니다. 나는 결과물을 얻지 못하고있다. – shshaw

+0

나는 이걸'getContent' 더미 함수 (그냥 var_dumps 입력)로 이렇게 실행할 수있었습니다. 어떤 오류나 경고를 받고 있습니까? 아니면 함수가 아무 것도하지 않고 있습니까? – Vatev

+0

제공된 편집이 완벽하게 작동합니다. 고맙습니다! – shshaw

1

의 핵심 기능입니다. 현재 가지고 계신 것은 article, display 등등과 $myStyle (배열이어야 함)입니다. 충분한 인수를 전달하지 않았기 때문에 오류가 발생합니다.

가능한 경우 getContent()에 기본 매개 변수가 있는지 확인하고 확인하십시오.

1

당신은 당신에게 자신의 기능을 만들 수 있습니다 단지 당신이 필요로하는 인수를하고 그 인수 플러스 기본값 하나와의 getContent() 함수를 호출 . 대신의 getContent()의 새로운 기능을 사용

편집 :

당신은 이런 일을 수행

<?php 
function myGetContent($param1,$param2,$param3,$param4,$param5,$param6){ 
    return getContent(
    $param1, 
    $param2, 
    $param3, 
    $param4, 
    $param5, 
    $param6, 
    "show:<div class='box box_stories'>", 
    "show:<div class='box_pad box_img bwWrapper'>", 
    "show:<div class='box_inner_pad'>", 
    "show:<h3><a href='/staff-detail/__slug__'>__authorX__</a></h3>", 
    "show:<p class='teamsummary'>__summary__</p>", 
    "show:<p class='phone_icon'>__authorXworkphone__</p>", 
    "show:</div>", 
    "show:</div>", 
    "show:</div>" 
); 
} 

을 한 다음 전화 :

myGetContent(
    "article", 
    "display:list", 
    "find_series:team", 
    "find_category:leadership-team", 
    "order:series", 
    "find_parent_category:team" 
); 
+0

조금 더 자세히 설명해 주시겠습니까? 나는'getContent()'함수에 표준 인자를 어떻게 전달할 것인지 정확히 모르겠다. 내가 같은 문제에 직면하고있는 것처럼 보입니다. – shshaw

+0

예 : – Oussama

+0

@ Yazmat로 응답을 편집 했으니'func_get_args()'로 일반화하고 싶을 것입니다. 누군가가 다음과 같이 그것을 복사 할 수 있습니다 : P – Vatev

0

이 필요 ?

<?php 

getContent(
    array(
     'type'     => 'article', 
     'display'    => 'list', 
     'find_series'   => 'team', 
     'find_category'   => 'leadership-team', 
     'order'     => 'series', 
     'find_parent_category' => 'team' 
    ), 
    array(
     '<div class="box box_stories">', 
     '<div class="box_pad box_img bwWrapper">', 
     '<div class="box_inner_pad">', 
     '<h3><a href="/staff-detail/__slug__">__authorX__</a></h3>', 
     '<p class="teamsummary">__summary__</p>', 
     '<p class="phone_icon">__authorXworkphone__</p>', 
     '</div>', 
     '</div>', 
     '</div>' 
    ) 
); 


function getContent($argv, $shows){ 
    echo '<p>'.$argv['type'].'</p>'; 
    echo '<p>'.$argv['list'].'</p>'; 
    /* ... etc ... */ 

    echo '<pre>'.htmlspecialchars(print_r($argv, true), ENT_QUOTES).'</pre>'; 
    echo '<pre>'.htmlspecialchars(print_r($shows, true), ENT_QUOTES).'</pre>'; 
} 
+0

불행히도 나는'getContent' 함수 자체를 변경할 수 없습니다. CMS의 핵심 부분입니다. – shshaw