将函数值传递给同一类中的另一个函数 [英] passing function values to another function in same class

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

问题描述

我想将函数值传递给同一类中的另一个函数,例如将一些值存储在变量中,然后在同一类中的另一个函数中调用此变量。

I want to pass function values to another function in the same class, like to store some values in a variable and then call this variable in another function in the same class.

这是我的代码

public function ven_coupon()
    {
        if ($_POST) {
            $number = $_POST['coupon'];
            $query = $this->front->ven_coupon($number);
            if (count($query) <= 0 ) {
                echo "Not Valid";
            }
            $payment = $this->cart->total();
            $dis = $query['discount'];
            $name = $query['username'];
            $number = $query['number'];
            $discount['discount'] = ($payment*$dis)/100;
            $discount['data'] = $dis;
            $this->load->view('checkout',$discount);
        }
    }

    public function addcart()
    {
        $ven = $this->ven_coupon();
        echo $ven($name);
        echo $ven($dis);
        echo $ven($number);
    }


推荐答案

您可以创建字段(变量),则需要在函数外部使用它们,然后使用 this 关键字使用它们。例如:

You could create the fields(variables) you need outside the function then use them using the this keyword. For example:

private $dis;
private $name;
private $number;

public function ven_coupon()
{
    if ($_POST) {
        $number = $_POST['coupon'];
        $query = $this->front->ven_coupon($number);
        if (count($query) <= 0 ) {
            echo "Not Valid";
        }
        $payment = $this->cart->total();
        $this->dis = $query['discount'];
        $this->name = $query['username'];
        $this->number = $query['number'];
        $discount['discount'] = ($payment*$dis)/100;
        $discount['data'] = $dis;
        $this->load->view('checkout',$discount);
    }
}


public function addcart()
{
    $ven = $this->ven_coupon();
    echo $this->name;
    echo $this->dis;
    echo $this->number;
}

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

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