dpr77335 2013-02-15 05:23
浏览 106
已采纳

如何将PHP变量从一个函数传递到另一个函数?

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

}
  • 写回答

2条回答 默认 最新

  • dongyan1974 2013-02-15 05:51
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。