2017-08-23 10 views
0

GoogleアナリティクスAPIコードをOOPの形式で入れていますが、アクセスクラスを作成しましたが、各関数から返される変数にアクセスするのに問題があります。例:他の関数から値を取得する方法 - PHP

accessクラスには、initializeAnalytics();という機能があります。

function initializeAnalytics(){ 

$KEY_FILE_LOCATION = __DIR__ . '/service-account-credentials.json'; 

$client = new Google_Client(); 
$client->setApplicationName("xxxxxx"); 
$client->setAuthConfig($KEY_FILE_LOCATION); 
$client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']); 
// $client->setRedirectUri($this->_redirectURI); 
$client->setAccessType('offline'); 
$client->setApprovalPrompt('force'); 
$analytics = new Google_Service_Analytics($client); 

return $analytics; 
} 

私が持っていた後、クラスAccessAnalytics();

function acessaAnalytics(){ 
     global $results; 
     $analytics = new access(); 
     $analytics->initializeAnalytics(); 
     $this->getFirstProfileId(); 
     $results = getResults($analytics, $profile); 
     return $results; 
    } 

そして最後にクラスgetFirstProfileId();

function getFirstProfileId($analytics) { 

    $accounts = $analytics->management_accounts->listManagementAccounts(); 

    if (count($accounts->getItems()) > 0) { 
     $items = $accounts->getItems(); 
     $firstAccountId = $items[0]->getId(); 

     // Get the list of properties for the authorized user. 
     $properties = $analytics->management_webproperties 
      ->listManagementWebproperties($firstAccountId); 

     if (count($properties->getItems()) > 0) { 
      $items = $properties->getItems(); 
      $firstPropertyId = $items[0]->getId(); 

      // Get the list of views (profiles) for the authorized user. 
      $profiles = $analytics->management_profiles 
       ->listManagementProfiles($firstAccountId, $firstPropertyId); 

      if (count($profiles->getItems()) > 0) { 
      $items = $profiles->getItems(); 

      // Return the first view (profile) ID. 
      return $items[0]->getId(); 

      } else { 
      throw new Exception('No views (profiles) found for this user.'); 
      } 
     } else { 
      throw new Exception('No properties found for this user.'); 
     } 
     } else { 
     throw new Exception('No accounts found for this user.'); 
     } 

     return $items; 
    } 

、どのようにお互いを補完する機能間のこれらの値を得るのですされる問題?前もって感謝します!

答えて

0

1)これらをグローバル変数として設定すると、それらを呼び出すだけですべての関数で使用できるようになります。

2)関数から変数を返します。 return $ analytics; $ NewVariable = initializeAnalytics(); この関数を呼び出すと、 が割り当てられます。

これは私がこの問題を考える2つの解決策です。

関連する問題