2015-04-29 55 views
0

古怪。任何想法爲什麼找不到課程?PHP使用關鍵字命名空間未找到類

目錄樹:

test2.php 
- src 
    - Google 
    - Spreadsheet 
     DefaultServiceRequest.php 
     ServiceRequestInterface.php 
     Google_Client.php 
     ... 

test2.php:如果

namespace src\Google\Spreadsheet; 

require_once 'src/Google/Spreadsheet/ServiceRequestInterface.php'; 
require_once 'src/Google/Spreadsheet/DefaultServiceRequest.php'; 
require_once 'src/Google/Spreadsheet/Google_Client.php'; 

use Google\Spreadsheet\ServiceRequestInterface; 
use Google\Spreadsheet\DefaultServiceRequest; 
use Google\Spreadsheet\ServiceRequestFactory; 

function getGoogleTokenFromKeyFile($clientId, $clientEmail, $pathToP12File) { 
    require 'src/Google/Spreadsheet/Google_Client.php'; 
    $client = new Google_Client(); 
    $client->setClientId($clientId); 

    $cred = new Google_Auth_AssertionCredentials(
     $clientEmail, 
     array('https://spreadsheets.google.com/feeds'), 
     file_get_contents($pathToP12File) 
    ); 

    $client->setAssertionCredentials($cred); 

    if ($client->getAuth()->isAccessTokenExpired()) { 
     $client->getAuth()->refreshTokenWithAssertion($cred); 
    } 

    $service_token = json_decode($client->getAccessToken()); 
    return $service_token->access_token; 
} 

$serviceRequest = new DefaultServiceRequest(getGoogleTokenFromKeyFile(..., ..., ...)); 
ServiceRequestFactory::setInstance($serviceRequest); 

不知道這是谷歌相關的API或內容。有些奇怪的是ServiceRequest類只有在我需要它們時纔會起作用。當我沒有,它說它找不到它...當我嘗試添加src /到使用路徑,沒有工作,我試圖一起去除路徑,都沒有做任何事情。

錯誤:致命錯誤:類「SRC \谷歌\電子表格\ Google_Client」在test2.php沒有發現第15行

+1

使用[autoload](http://php.net/manual/en/language.oop5.autoload.php),test2不應該有這樣的名稱空間 – cske

+1

是的,你應該自動加載這些,不需要他們。檢查你的include路徑是否定義了一個'__autoload'方法。如果您確實需要這些文件,請使用'require_once'。你在這裏有一個'require'可能會導致問題。 –

回答

2

看起來像您使用this

,如果你使用的作曲家安裝庫您需要包括vendor/autoload.php文件在你的代碼

require 'vendor/autoload.php'; 

它recomneded您使用作曲家安裝此庫,但如果你不希望使用作曲家,你需要創建自動加載機和require它在你的代碼