如何在Yii中实例化另一个命名空间中的对象?

时间:2015-12-15 18:56:14

标签: php yii2 composer-php google-adwords

我有以下

<?php
namespace app\commands;
use \Keyword as GoogleKeyword;

class KwController extends \yii\console\Controller
{

  public function actionTest() {
      $keyword = new GoogleKeyword();
  }

它给出了错误

$ yii kw/test
PHP Fatal error:  Class 'Keyword' not found in /cygdrive/c/Users/Chloe/workspace/project/commands/KwController.php on line 69
PHP Fatal Error 'yii\base\ErrorException' with message 'Class 'Keyword' not found'

in /cygdrive/c/Users/Chloe/workspace/project/commands/KwController.php:69

Stack trace:
#0 [internal function]: yii\base\ErrorHandler->handleFatalError()
#1 {main}

我不明白因为它曾经工作过。

这是定义的地方

$ grep Keyword vendor/googleads/googleads-php-lib/src/Google/Api/Ads/AdWords/v201509/CampaignCriterionService.php
if (!class_exists("Keyword", false)) {
  class Keyword extends Criterion {
    const XSI_TYPE = "Keyword";

以下是composer.json

{
    "require": {
        "googleads/googleads-php-lib": "~6.5"

1 个答案:

答案 0 :(得分:0)

以下任何一项工作

use \Keyword as GoogleKeyword; // name clash
require 'vendor/googleads/googleads-php-lib/src/Google/Api/Ads/AdWords/v201509/CampaignCriterionService.php'; # for Keyword

  public function actionTest() {
    $gaw = new GoogleAdWords();
    $user = $gaw->getUser(); # returns an AdWordsUser
    $campaignCriterionService = $user->GetService('CampaignCriterionService', ADWORDS_VERSION);
    $keyword = new GoogleKeyword();

我认为GetService自己做了一些神奇的装载。