簡體   English   中英

如何在php中使用命名空間類

[英]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()用作靜態函數,因為它不是靜態的。 您應該將其設為靜態(通過在publicfunction之間放置static )或在對象上使用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM