簡體   English   中英

如何將PHP變量從一個函數傳遞給另一個函數?

[英]How to pass a PHP variable from one function to another?

我想將變量($q_view)從函數q_view_main傳遞給函數sidebar 還搜索stackoverflow尋找解決方案,但不了解。

兩種功能在同一類中。

function main()
{       
    $content=$this->content;

    $this->main_parts($content);

    $this->suggest_next();

    $this->widgets('main', 'bottom');

    $this->main_sidebar();
}
function main_sidebar()
{
    $content=$this->content;
    $main_class = ((!empty ($content['widgets']['main']['high'])) ? 'twelve' : 'nine');
    if (!empty ($content['widgets']['main']['high'])){                              
        $this->output('<DIV class="main-sidebar three columns">', '');
        $this->post_tags($q_view_for_sidebar, 'qa-q-view'); //want 
        $this->widgets('main', 'high');

        $this->output('</DIV>');
    }
}
function main_parts($content)
{
    foreach ($content as $key => $part) {
        $this->set_context('part', $key);
        $this->main_part($key, $part);
    }
}
function main_part($key, $part)
{
    if (strpos($key, 'custom')===0)
        $this->output_raw($part);

    elseif (strpos($key, 'form')===0)
        $this->form($part);

    elseif (strpos($key, 'q_list')===0)
        $this->q_list_and_form($part);

    elseif (strpos($key, 'q_view')===0)
        $this->q_view($part);

    elseif (strpos($key, 'a_form')===0)
        $this->a_form($part);

    elseif (strpos($key, 'a_list')===0)
        $this->a_list($part);

    elseif (strpos($key, 'ranking')===0)
        $this->ranking($part);

    elseif (strpos($key, 'message_list')===0)
        $this->message_list_and_form($part);

    elseif (strpos($key, 'nav_list')===0) {
        $this->section(@$part['title']);        
        $this->nav_list($part['nav'], $part['type'], 1);
    }
}
function q_view_main($q_view)
{           
    $this->q_view_stats($q_view);           
    $this->post_meta_author($q_view, 'qa-q-view');
    $this->q_view_content($q_view);
    $this->q_view_extra($q_view);
    $this->q_view_follows($q_view);
    $this->q_view_closed($q_view);


    $this->post_tags($q_view, 'qa-q-view');         
    $this->post_meta_other($q_view, 'qa-q-view');
    $this->q_view_buttons($q_view);
    $this->c_list(@$q_view['c_list'], 'qa-q-view');
    $this->output('</DIV>');

}

如果兩個函數都位於同一個類中,則可以使用$ this調用另一個函數中的另一個函數,如其他答案所示

class myclass{

public $var1 = 121;
    public function foo($postid){
        echo $postid;
    }
    public function bar(){
        $this->foo($this->var1);
    }
}
$objectmyclass = new myclass();
$objectmyclass->bar();

要么

class myclass{

public $var1 = 122;
    public function foo($postid){
        echo $postid;
    }
    public function bar($postid){
        $this->foo($postid);
    }
}
$objectmyclass = new myclass();
$objectmyclass->bar($objectmyclass->var1);

兩種方式都可以執行foo函數中的語句,希望這對您有所幫助

如果我不想誤會你,只需在側邊欄函數中調用post函數

function sidebar($postID)
{
    $postID=$this->post($postID);
    $this->output($this->content);
    $this->tag($postID); //get $postID from sidebar
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM