【问题标题】:PHP Class Function returning a variable in a new file with-in a new functionPHP类函数在新函数中返回新文件中的变量
【发布时间】:2012-11-01 16:20:42
【问题描述】:

我需要一些帮助来解决我遇到的这个疯狂问题

我创建了一个类,我使用 Curl 从 URL 获取数据,并使用该信息 $_GET 为每个变量一个值。

所以我有这个类,我正在使用一个新文件来获取发布在新函数中的项目的 $name 但每次我将短语变量放入新函数中时,我都会得到 NULL 这里是我的类-短

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 它将为空,我不确定如何返回 $ this->name ($parsedResponse['trackName']) 是全局的,供新函数等中的所有使用,因为我的班级使用公共 getData() 然后 $this->name (getData()->name)

这仅适用于任何函数,因此如果有函数,则设置为 name 的变量将仅具有高于函数的值,我希望这是可以理解的。

不确定我是否需要在 $this->name 中返回或什么,因为我尝试在 getName() 下面的类中创建一个新的公共函数,结果也为 null,因此我无法设置a = 语句中的公共变量,因为它会从 try{} 中捕获(e),询问它期望 T_FUNCTION 而不是 T_VARIABLE 即 public $name = $name; // 错误 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"

/* 我不能用变量做到这一点:( 例如 $this->name / $parsedResponse['trackName'] */

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

【问题讨论】:

  • 您可以使用save_image($img,$name,$parseReponse)等参数调用新函数
  • 之前也尝试过,但我仍然得到 NULL,知道如何在 save_image() 函数中使用它来输出 $this->name 吗?
  • $img 的值应该是多少
  • class 中设置$this-> 名称后,您可以通过在save_image($name_of_class_instance->name) 中传递变量或使用global $name_of_class_instance->name 启动函数来在其他函数中使用该变量.

标签: php


【解决方案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);

【讨论】:

  • 很棒的人,但我厌倦了它,它返回 catch(e) 作为错误,使其无法从 $appIdentity 中加载 URL throw new AppStoreService_Exception("mInvalid data 从 AppStore 返回Web 服务。检查您的应用 ID,并确认应用商店当前已启动。");每次加载:(你认为这可能是代码或我做错了什么?
  • 我会检查你的逻辑,$parsedResponse['resultCount'] 按预期返回1。你可以通过var_dump($parsedResponse['resultCount'])来显示价值。
猜你喜欢
  • 2013-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-30
  • 2017-09-23
  • 1970-01-01
相关资源
最近更新 更多