USING $THIS in PHP

后端 未结 2 1394
旧巷少年郎
旧巷少年郎 2021-01-29 07:26

In my code I\'m trying to call a function that is inside of another function. I\'m trying to pass the $results var to the NVPToArray() function and return the result. After that

相关标签:
2条回答
  • 2021-01-29 07:54

    You shouldn't declare a function within a function in PHP, it will cause an error if you run the function more than once. Also, your function is named NVPToArray, but you are calling NVPT. Try something more like this (it's not entirely clear what you are trying to do in postPurchase).

    <?php
    
    class Process extends BaseController {
    
        // Function to convert NTP string to an array
    
        public function postPurchase() {
            // Include config file
            include(app_path().'/includes/paypal_config.php');
            $result = curl_exec($curl);
            curl_close($curl);
    
            // Parse the API response
            $nvp_response_array = parse_str($result);
    
            echo $this->NVPToArray($result);
        }
    
        // Function to convert NTP string to an array
        public function NVPToArray($NVPString)
        {
            $proArray = array();
            while(strlen($NVPString))
            {
                // name
                $keypos= strpos($NVPString,'=');
                $keyval = substr($NVPString,0,$keypos);
                // value
                $valuepos = strpos($NVPString,'&') ? strpos($NVPString,'&'): strlen($NVPString);
                $valval = substr($NVPString,$keypos+1,$valuepos-$keypos-1);
                // decoding the respose
                $proArray[$keyval] = urldecode($valval);
                $NVPString = substr($NVPString,$valuepos+1,strlen($NVPString));
           }
           return $proArray;
        }
    }
    
    ?>
    
    0 讨论(0)
  • 2021-01-29 08:02

    You are getting undefined function because you are calling NVPT() which not exists.

    You should call NVPToArray() function.

    And about the $this, as your $result var is defined inside the function you don't need to use it. You can call it with just $result.

    You would use it, if you had something like:

    class T {
    ...
        public $result;
    
        function yourF(){
             ....
             echo $this->result;
        }
    }
    
    0 讨论(0)
提交回复
热议问题