PHP类函数使用新函数在新文件中返回变量

时间:2012-11-13 08:09:45

标签: php

我需要帮助解决这个疯狂的问题

我有一个类,我使用Curl从URL获取数据并使用该信息$ _GET的每个变量都是一个值。

所以我有了这个类,并且我正在使用一个新文件来获取在新函数中发布的项的$ name但是每次我将phrased变量放在新函数中我得到NULL这里是我的类-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);
    */

}

基本上如果你可以看看我是否尝试添加我的类变量来获取新函数中appIdentity的名称,save_image它将为null并且我不确定我将如何返回$ this-> name($ parsedResponse ['trackName'])在新函数等中使用全局,因为我的类使用public getData()然后是$ this-> name(getData() - > name )

这只能解决所有函数的问题,所以如果有一个函数,设置为name的变量只会在函数之上有值,我希望这是可以理解的。

不确定我是否需要在$ this->名称中返回,或者因为我尝试在getName()下面的类中创建一个新的公共函数,并且因此我也不能在=语句中设置公共变量,因为它会从try {}中捕获(e),要求它期望T_FUNCTION而不是T_VARIABLE,即public $ name = $ name; // error public $ name ='Test'; //作品'{$ name}'不起作用

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.g $ this-> name / $ parsedResponse ['trackName'] * /

如果有人能帮助我,我将不胜感激

1 个答案:

答案 0 :(得分:1)

创建类的实例并运行设置name变量的函数。

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

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

然后,您可以在类之外的其他函数中使用生成的$ name变量:

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

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

或者......将它传递给你想要使用它的任何函数

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

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