如何将PHP变量从一个函数传递给另一个函数? [英] How to pass a PHP variable from one function to another?

查看:169
本文介绍了如何将PHP变量从一个函数传递给另一个函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将函数 q_view_main 中的变量($ q_view)传递给函数边栏
也搜索了stackoverflow的解决方案,但不明白它。



这两个函数都在同一个类中。



<$ ()
{
$ content = $ this-> content;

$ this-> main_parts($ content);

$ this-> suggest_next();

$ this->小部件('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三列>','');
$ this-> post_tags($ q_view_for_sidebar,'qa-q-view'); //想要
$ this->小部件('main','high');

$ this-> output('< / DIV>');
}

function main_parts($ content)
{
foreach($ content as $ key => $ part){
$ this- > set_context('part',$ key);
$ this-> main_part($ key,$ part);
}

函数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->排名($ part);

elseif(strpos($ key,'message_list')=== 0)
$ this-> message_list_and_form($ part);
$ b $ elseif(strpos($ key,'nav_list')=== 0){
$ this-> section(@ $ part ['title']);
$ this-> nav_list($ part ['nav'],$ part ['type'],1);


函数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();

OR

  class myclass {

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

这两种方法都可以执行函数foo中的语句
希望这有助于

I want to pass variable ($q_view) from a function q_view_main to a function sidebar. Also searched stackoverflow for solution but dont understand it.

Both functions are in same class.

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>');

}

解决方案

If both of your function are residing in the same class you can call a function inside another function using $this as pointed in other answers

class myclass{

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

OR

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);

both ways you will be able to execute the statements inside the function foo hope this helps

这篇关于如何将PHP变量从一个函数传递给另一个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆