【问题标题】:how to use the namespace classes in php如何在 php 中使用命名空间类
【发布时间】:2015-07-28 05:25:30
【问题描述】:

我有一个带有命名空间的类,它还需要许多其他类。主类是

<?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 命名空间。有朋友能告诉我解决方法吗??

谢谢。

【问题讨论】:

    标签: php oop namespaces


    【解决方案1】:
    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;
    

    【讨论】:

    • 致命错误:在第 5 行的 /var/www/pdfparser/asposetry/index.php 中找不到类 'Aspose\Cloud\Pdf\Document' 是错误
    • @ris 你要么必须include 每个类文件,要么定义一个自动加载器来为你做这件事。看这里:php.net/manual/en/language.oop5.autoload.php
    【解决方案2】:

    您正在尝试使用位于 Aspose\Cloud\Pdf 命名空间内的 Document 类,但实际上您使用的是没有命名空间的 Document 类。您必须使用以下方式之一:

    //Option one:
    use Aspose\Cloud\Pdf\Document;
    Document::getFormFields();
    
    //Option two:
    Aspose\Cloud\Pdf\Document::getFormFields();
    

    还请注意,您不能将Document::getFormFields() 用作静态函数,因为它不是静态的。您应该将其设为静态(将static 放在publicfunction 之间)或在对象上使用它。

    【讨论】:

    • 致命错误:在第 5 行错误的 /var/www/pdfparser/asposetry/index.php 中找不到类 'Aspose\Cloud\Pdf\Document' 错误
    • 您可能忘记在应用程序的某处将文件实际包含在 Document 类中。使用自动加载或手动包含文件:include('Aspose/Cloud/Pdf/Document.php');
    • 现在错误移至下一课。我必须在每个班级都包括班级???这是现在的错误致命错误:第 365 行的 /var/www/pdfparser/asposetry/Aspose/Cloud/Pdf/Document.php 中找不到类 'Aspose\Cloud\Pdf\Product'
    • 也许你应该考虑一些自动加载器来自动加载你的类?标准 PHpphp.net/manual/de/language.oop5.autoload.php
    • 是的,您要么必须单独包含每个类(尽管在您的应用程序中只包含一次),要么按照@AdamM 的建议使用自动加载器。当你使用一个类时,自动加载器会检查你是否已经加载(包含)了一个类。如果没有,它将为您提供。
    猜你喜欢
    • 2020-11-17
    • 2016-01-05
    • 1970-01-01
    • 2016-11-08
    • 1970-01-01
    • 1970-01-01
    • 2012-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多