[英]how to use the namespace classes in php
我有一個帶有名稱空間的類,它進一步需要許多其他類。 主要是
<?php
/**
* Deals with PDF document level aspects.
*/
namespace Aspose\Cloud\Pdf;
use Aspose\Cloud\Common\AsposeApp;
use Aspose\Cloud\Common\Product;
use Aspose\Cloud\Common\Utils;
use Aspose\Cloud\Event\SplitPageEvent;
use Aspose\Cloud\Exception\AsposeCloudException as Exception;
use Aspose\Cloud\Storage\Folder;
class Document
{
public $fileName = '';
public function __construct($fileName='')
{
$this->fileName = $fileName;
}
/**
* Gets the page count of the specified PDF document.
*
* @return integer
*/
public function getFormFields()
{
//build URI
$strURI = Product::$baseProductUri . '/pdf/' . $this->getFileName() . '/fields';
//sign URI
$signedURI = Utils::sign($strURI);
//get response stream
$responseStream = Utils::ProcessCommand($signedURI, 'GET', '');
$json = json_decode($responseStream);
return $json->Fields->List;
}
}
我在index.php中使用這樣的
<?
ini_set('display_errors', '1');
use Aspose\Cloud\Pdf;
$document=new Document;
echo $document->GetFormFields();
//or like this
echo Document::GetFormFields();
//also tried this
echo pdf::GetFormFields();
?>
錯誤
Fatal error: Class 'Document' not found in /var/www/pdfparser/asposetry/index.php on line 5
文檔類路徑為Aspose / Cloud / Pdf / Document.php
嘗試一個
如果我用來包括在index.php include(Aspose/Cloud/Pdf/Document.php)
,則工作,但隨后的名稱空間產生錯誤。 用include
更改每個use
名稱空間非常困難。 任何預算都可以告訴我解決方案嗎?
謝謝。
namespace Aspose\Cloud\Pdf;
class Document {
...
要use
此類,您必須編寫
use Aspose\Cloud\Pdf\Document
您也可以不use
語句來訪問它,但是每次都必須寫全名:
$document=new Aspose\Cloud\Pdf\Document;
// Or if you're in a namespace, you'll have to do this:
$document=new \Aspose\Cloud\Pdf\Document;
您正在嘗試使用Document
類是內部Aspose\\Cloud\\Pdf
命名空間,但是你實際上是使用一個Document
類沒有命名空間。 您必須使用以下方式之一:
//Option one:
use Aspose\Cloud\Pdf\Document;
Document::getFormFields();
//Option two:
Aspose\Cloud\Pdf\Document::getFormFields();
另外請注意,您不能將Document::getFormFields()
用作靜態函數,因為它不是靜態的。 您應該將其設為靜態(通過在public
和function
之間放置static
)或在對象上使用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.