1

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.

Here is my Code

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);
    }
1
  • you could use class properties. any function inside class will be able to set values to these properties, and some other function can use these new values. Commented Mar 6, 2016 at 20:08

3 Answers 3

1

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

i 'm using construct function in my code that's why global variables can't work
0

Scope of variable is inside the function. You need to made variable as a part of class as:

Basic Example:

class yourClass{
   private $name;

   public function functionA(){
      $this->name = "devpro"; // set property
   }

   public function functionB(){
      self::functionA();
      echo $this->name; // call property
   }
}

Comments

0

your function ven_coupon() doesn't return anything, therefore $ven is empty when you read it in your function addcart().

in order to pass several variables from one function to another, you need to create an array.

function ven_coupon(){
   $dis = $query['discount'];
   $name = $query['username'];
   $data=array('dis'=>$dis,'name'=>$name)
   return $data
}
function addcart()
{
    $ven = $this->ven_coupon();
    echo $ven['name'];
    //etc.
}

Edit: as you are already using an array $query you could simply return $query; in function ven_coupon and then read it in your function addcart()

1 Comment

please tell me how i return all these values in addcart() function. can you please give me code demo thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.