Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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, I want to echo the result, which will be an array. I keep getting an error saying that the function is undefined so I'm pretty sure that I'm not calling the function properly.

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

                // Function to convert NTP string to an array
                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;
                }

                NVPToArray($this->$result);

                echo $proArray;

        }


}

?>
share|improve this question
2  
looks like you're trying to call the function NVPT not NVPToArray() ... also, I recommend against nesting functions like that as it makes code much less readable. –  John C Jul 5 at 1:12
    
Why are you defining NVPToArray inside the other function ? Why don't you put it in the root of the class ? –  1nflktd Jul 5 at 1:18
    
Does your edit indicate that NVPT() was a typo in your original post? –  faintsignal Jul 5 at 1:18

2 Answers 2

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;
    }
}
share|improve this answer

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;
    }
}

?>
share|improve this answer
    
+1 This code is way better organized than the original code OP posted. –  1nflktd Jul 5 at 1:25
    
I fixed the code with your improvements. The code I previously uploaded was a trimmed version of the original. Here is the original. I'm trying to call the property "nvp_response_array" and call the NVPToArray() on it but for some reason it's not working. –  user3216221 Jul 5 at 2:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.