How to Include third party library into plugin

Hi,

I have installed a third party library (Google api PHP Client) with composer and now I don’t know how to include it in my plugin. (Piwik version is 3.0.2, PHP 7.1.x)

Under the plugin directory I run:

composer require google/apiclient:^2.0

The question: how can I use/include Google_Client class into Controller.php class? What’s the class path?

My Controller.php

<?php
namespace Piwik\Plugins\MyPlugin;
use Piwik\View;
require __DIR__ . "/vendor/autoload.php"; //autoloader
class Controller extends \Piwik\Plugin\Controller
{
    public function index()
    {
        $client = new Google_Client(); //test Goggle_Client class
        return $this->renderTemplate('index', array(
            'answerToLife' => 42
        ));
    }
}

The error returned is:
The following error just broke Piwik (v3.0.2): Class ‘Piwik\Plugins\MyPlugin\Google_Client’ not found

Thanks in advance!

In this case you should have a file Google_Client.php filled with the same name class that extends the original Google_Client using namespace. That is to add your custom functionality to the lib.

If you want to go raw with no class extension, just use all the namespace in new statement

for example: $client = new \Google\Whatever\Google_Client;