How to pass objects in php function

This is the simple way to pass object in php  function .Before run these codes you should aware of following files.
1. Khunt.php :-                     This is the main class file.
2. ReqMsgDTO.php :-          This file have setter and getter properties.
3. Sale.html :-                       This file basically provides the User Interface along with Order Id and Mid.
4. test.php  :-                        This file will  invoke the generateTrnReqMsg($object) along with pass object .                                         

1. Khunt.php

<?php 
include("ReqMsgDTO.php");

class Khunt
    {

  function generateTrnReqMsg($reqMsgDTO)
        {
       
       
       
            try
            {
                if ($reqMsgDTO == null || $reqMsgDTO->Mid == null || $reqMsgDTO->Mid == ""
                || $reqMsgDTO->OrderId == null || $reqMsgDTO->OrderId == ""             
                   )
                {
                   
                    $reqMsgDTO->set_statusDesc("Mandatory fields are missing");
                    return $reqMsgDTO;
                }             
               
              
               
            $merchantReqStr= $reqMsgDTO->Mid ."|". $reqMsgDTO->OrderId ;
            $strings=explode("|",$merchantReqStr);
            //echo "<pre>"; print_r($strings); die;
            $reqMsgDTO->setMid($strings[0]);
            $reqMsgDTO->set_orderId($strings[1]) ;   
                                
            }
            catch (Exception $ex)
            {
              
               // $reqMsgDTO->set_statusDesc("Error Occured during Creating request message");
                //throw new Exception($ex->getMessage());
              
            }
            return $reqMsgDTO;
        }
       
       
   
    }



?>
2. ReqMsgDTO .php:-

<?php


class ReqMsgDTO {

    private $mid;
    private $orderId;

   
   
   
public function getMid() {
    return $this->mid;
}


public function setMid($mid) {
    $this->mid = $mid;
}
       
   
public function get_OrderID() {
    return $this->orderId;
}

public function set_OrderID($OrderId) {
    $this->orderId = $orderId;
}



}
?>

3. Sale.html:-
<html>
<head>
<meta charset="utf-8">
<title>Knowladge Hunt</title>
</head>


<body >
<form action="test.php" method="post">
<div >
        <h4><label>ORDER NO</label><br><input type="text" class="form-control" name="hdnOrderID" id="hdnOrderID" value="12" /></h4>
      
        <h4 style="border:none"><label>Mid</label><br> <input type="text" class="form-control" name="mid" id="mid" value="100"/></h4>
        <input type="submit" class="btn btn-danger btn-block" style="background:#fa5400;padding:10px;font-size:21px"
        name="CHECKOUT" value= "CHECKOUT" onclick="return getData();"/>
    </div>
</form>
</body>
</html>

4.test.php:-

<?php

        include("Khunt .php");
        $obj=new Khunt ();  // Created the instance of  Khunt class
        $reqMsgDTO=new ReqMsgDTO(); // Created the instance of  ReqMsgDTO class
        $reqMsgDTO->Mid=$_POST['mid'];  // initialize the ReqMsgDTO Object .
        $reqMsgDTO->OrderId =$_POST['hdnOrderID'];  //  initialize the ReqMsgDTO Object .
       
   
        $urlParameters=$obj->generateTrnReqMsg($reqMsgDTO); // passed the ReqMsgDTO Object  in to the generateTrnReqMsg() function.
        echo "Merchant ID=".$encVal=$urlParameters->Mid; //out put 12
        echo "<br>";
        echo "Order ID=".$encVal=$urlParameters->OrderId; //out put 100        
       
       
       
       
?>

Comments