2013-04-26 2 views
2

PHP에서 작업 중이며 내부에서 다음 함수를 호출하면 foreach{} 루프가됩니다. 이 함수는 불필요한 코드 반복을 피하기 위해 옵션 매개 변수로 $subTheme을 수락해야합니다 (DRY, 맞습니까?). -PHP : 배열 선언의 변수

그래서, 우선 여기 함수의 :

/* 
* CSS needed to apply the selected styles to text elements. 
*/ 
function this_site_text_css($subTheme = '') { 

    $GLOBALS['this_theme_mod']; // this is in global scope already 

    $themeVarSuffix = $subTheme['var_suffix']; 
    $themeClassName = $subTheme['class_name']; 

    $content_bg_color = $this_theme_mod['content_bg_color' . $themeVarSuffix ]; 
    $page_bg_color  = $this_theme_mod['page_bg_color' . $themeVarSuffix ]; 
    $primary_color  = $this_theme_mod['primary_theme_color' . $themeVarSuffix ]; 

    $css = 'body' . $themeClassName . '{ /* special classes */ };' 

    return $css 
} 

일이 더있다하지만 오히려 지루한 그냥 반환하는 문자열로 CSS를 연결합니다.

은 내가 PHP 경고 Illegal string offset 'class_name' 받고 있어요 내가 PHP가 $themeClassName에 그것을 선언 할 때 나를 $themeVarSuffix 인라인을 연결하고 싶지 않기 때문입니다 추측하고있어이

$data = ''; 
$data .= this_site_text_css(); 
$subThemeArray = array(
    'theme_a' => array( 
    'var_suffix' => '_theme_a', 
    'class_name' => '.theme-a', 
), 
    'theme_b' => array( 
    'var_suffix' => '_theme_b', 
    'class_name' => '.theme-b', 
), 
); 
foreach($subThemeArray as $key => $theme) { 
    $data .= this_site_text_css($theme); 
} 

처럼 호출되는 것. 나는 이것을 할 수있는 방법이 있음을 확신하고 있으며, 나는 모든 것을 조사했다. 아마도 적절한 키워드를 검색하지 않았을 지 모르지만, 어떤 도움을 주시면 감사하겠습니다.

답변

3

불법 문자열 오프셋 'class_name에'

은 ... $subTheme 실제로 string 아닌 array 것을 의미한다.

$data .= this_site_text_css(); 

을 그래서 $subTheme 물론 전혀 인덱스 'class_name을'의이 빈 문자열 인 : 당신은 함수 선언 $subTheme = ''에 대한 기본 값을 가지고 있고, 여기를 호출하면 값을 전달하는 놓친 때문입니다 .

+1

감사를 호출해야합니다! 그것뿐이었습니다. 그냥해야했습니다. 'function this_site_text_css ($ subTheme = array()) – Brian

1

$subThemeArray 배열에서 인덱스 theme_classclass_name

+0

+1 잘 잡으세요! 다른 사람의 경우,이 대답 후에 질문이 업데이트되었음을 ​​유의하십시오. – hek2mgl

+0

잡힌 것을 고맙게 생각합니다.하지만 처음에 게시 한 잘못된 코드가 처음에는 작업 코드와 다르며 작업 코드가 잘못된 코드가 아닌 의사 코드 만 작성 했으므로 의사 코드를 작성했습니다. – Brian

+0

np, 문제는 코드가 디버깅 용으로 게시 될 때 오타가 무엇인지, 코드에서 실제 버그가 무엇인지를 결코 확신 할 수 없기 때문입니다. 어쨌든 기쁜 @ hek2mgl은 당신을 위해 알아 냈습니다. – raidenace