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

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

问题描述:

I have the following

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

class KwController extends \yii\console\Controller
{

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

It's giving the error

$ 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}

I don't understand because it used to work.

Here's where it's defined

$ 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";

Here is composer.json

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

Either of the following worked

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

or

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

I think the GetService does some magic loading on its own.