2

I have a class with namespace which require many other classes further . main class is

<?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;
    }
}

I am using this like this in 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();

?>

Error

Fatal error: Class 'Document' not found in /var/www/pdfparser/asposetry/index.php on line 5

Document class path is Aspose/Cloud/Pdf/Document.php

attempt one

working if i use to include in index.php include(Aspose/Cloud/Pdf/Document.php) but then further namespace produce error. Very difficult to change every use namespace with include. can anybudy tell me the solution for this ??

thanks.

Manoj Dhiman
  • 5,096
  • 6
  • 29
  • 68

2 Answers2

2
namespace Aspose\Cloud\Pdf;

class Document {
    ...

To use this class, you'll have to write

use Aspose\Cloud\Pdf\Document

You can also access it without a use statement, but then you'll have to write the full name every time:

$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;
Schlaus
  • 18,144
  • 10
  • 36
  • 64
  • Fatal error: Class 'Aspose\Cloud\Pdf\Document' not found in /var/www/pdfparser/asposetry/index.php on line 5 is the error – Manoj Dhiman May 16 '15 at 09:21
  • @ris You'll either have to `include` each class file, or define an autoloader to do it for you. Take a look here: http://php.net/manual/en/language.oop5.autoload.php – Schlaus May 16 '15 at 09:22
1

You are trying to use the Document class that's inside the Aspose\Cloud\Pdf namespace, but you are actually using a Document class without a namespace. You have to use one of the following ways:

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

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

Also note that you can't use Document::getFormFields() as a static function, because it is not static. You should make it static (by putting static between public and function) or use it on an object.

redelschaap
  • 2,774
  • 2
  • 19
  • 32
  • Fatal error: Class 'Aspose\Cloud\Pdf\Document' not found in /var/www/pdfparser/asposetry/index.php on line 5 error – Manoj Dhiman May 16 '15 at 09:21
  • You probably forgot to actually include the file with the Document class somewhere in your application. Use autoload or inlcude the file manually: `include('Aspose/Cloud/Pdf/Document.php');` – redelschaap May 16 '15 at 09:24
  • now error move to next class. i have to include classess in every class??? this is the error now Fatal error: Class 'Aspose\Cloud\Pdf\Product' not found in /var/www/pdfparser/asposetry/Aspose/Cloud/Pdf/Document.php on line 365 – Manoj Dhiman May 16 '15 at 09:32
  • maybe you should consider some autoloader for loading your classes automatically? Standard PHp http://php.net/manual/de/language.oop5.autoload.php – AdamM May 16 '15 at 09:34
  • Yes, you'll either have to include each class seperately (though only once in your application) or use an autoloader as @AdamM suggests. An autoloader checks whether you've already loaded (included) a class when you're using one. If not, it will include it for you. – redelschaap May 16 '15 at 09:36
  • where can i define __autoload function ?? i have defiend in index.php still same errors – Manoj Dhiman May 16 '15 at 09:47
  • Using an autoloader is not that simple when your classes are located in different folders. Take a look at http://stackoverflow.com/a/9628060/1997303 and try to adjust that to your needs. – redelschaap May 16 '15 at 09:55