BarakaAka1Only
BarakaAka1Only

Reputation: 55

PHP Class Function returning a variable in a new file with-in a new function

I'm in need of some help with this insane issue i'm having

I have a class i made that i'm using Curl to obtain data from a URL and with that information $_GET's each variable a value.

So i have the class and i'm using a new file to get the $name of the item posted in a new function but every-time i put the phrased variable in the new function i get NULL here is my class in-short

class AppStore {

  public $name;


public function getData() {
       $requestUrl = self::APPSTORE_LOOKUP_URL . $this->appIdentity;

       $ch = curl_init($requestUrl);
                curl_setopt($ch, CURLOPT_URL, $requestUrl);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
                curl_setopt($ch, CURLOPT_REFERER, "");
                curl_setopt($ch, CURLOPT_COOKIESESSION, true);
                curl_setopt($ch, CURLOPT_USERAGENT, $this->iTunesUserAgent);
                curl_setopt($ch, CURLOPT_ENCODING, 0);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_FAILONERROR, true);
                curl_setopt($ch, CURLOPT_TIMEOUT, 30);
                $response = curl_exec($ch);
                $errno = curl_errno($ch);
                curl_close($ch);


        $parsedResponse = json_decode($response, true);
        if ($parsedResponse['resultCount'] == 1)
         {
            $parsedResponse = $parsedResponse['results'][0];

            $this->name = $parsedResponse['trackName'];
             require_once('func.ini.php'); // new function to save images etc
        } else {
            throw new AppStoreService_Exception("mInvalid data returned from the AppStore Web Service. Check your app ID, and verify that the Appstore is currently up.");
        }

} 

}

// This is the func.ini.php code
echo $parsedResponse['trackName']; // Works here output equals a Name like Baraka etc
function save_image($img){

    $dirs = "data/Apps/".$name; // Gets the name of the ID in the appIdentity so we can use CURL to download images and move the images to new folder else it'll fail and won't move 
    $path = $dirs."ScreenShots";
    $fullp = basename($img);
    $fullpath = "$path/$fullp";
    $something = $dirs.'ScreenShots'

    var_dump($parsedResponse['trackName']); // Null same if i echo it i get a white page etc

    /* Sample Code 
    $ch = curl_init ($img);
    curl_setopt($ch, CURLOPT_URL,$img);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    */

}

Basically if you can see if I try to add my class's variable to get the Name of the appIdentity in the new function, save_image it'll be null and i'm not sure how I'll be able to return the $this->name ($parsedResponse['trackName']) to be global for all use in new functions etc since my class is using the public getData() then the $this->name (getData()->name)

this only works out side any functions so if there is a function, the variable set to name will only have value above the function(s) i hope this is understandable.

Not sure if i need to make a return in the $this->name or what because i tried making a new public function in the class below the getName() and that comes out null as well thus i can't set public variables in a = statement because it'll catch(e) from try{} asking it expects a T_FUNCTION than T_VARIABLE i.e public $name = $name; // error public $name = 'Test'; // works '{$name}' don't work

class Test {

public $test = 'Help me out please!';
  function GetMe() {
      echo $this->test;
  } 

}

$open = new Test;
echo $open->GetMe(); // Outputs "Help me out please"

/* i can't do that with a variable though :( i.g $this->name / $parsedResponse['trackName'] */

If anyone can help me out I'd be much appreciated

Upvotes: 1

Views: 120

Answers (1)

Luke
Luke

Reputation: 23700

Create an instance of your class and run the function that sets the name variable.

// Create an instance of AppName
$app_store = new AppName();

// Run the function
$app_store->getData();

You can then make use of the resulting $name variable created, within other functions outside of your class:

// By Making the instance global
function save_image()
{
    global $app_store;

    // Show the value of name
    echo $app_store->name;
}

OR... by passing it to any functions that you want to make use of it

function save_image2($name_var)
{
    echo $name_var;
}

// Called like
save_image2($app_store->name);

Upvotes: 1

Related Questions