Problem with namespace and include

Asked

Viewed 226 times

0

Good morning. I have a webservice made on slimPHP . I have a class called control, and he belongs to the namespace controllers, it works normally without problems , but there arose the need to import a class for PDF manipulation , so that when I give the error include it , saying that it did not find the requested class. Does anyone know what it might be ? Follow the class.

<?php

 namespace controllers {

  include "fpdf/fpdf.php";


   class Control {

    //Atributo para banco de dados
    private $PDO;

    /*
      __construct
      Conectando ao banco de dados
     */

      function __construct() {
        $this->PDO = new \PDO('mysql:host=localhost;dbname=infoged', 'root', 
              '*******'); //Conexão
                  $this->PDO->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); //habilitando erros do PDO
    }

    public function PDF($romaneio) {
       $pdf =  new \fpdf\FPDF("P", "pt", "A4");
       $pdf->AddPage();
    }
  }
 }

Error:

<b>Fatal error</b>:  Class 'fpdf\FPDF' not found in
<b>C:\xampp\htdocs\infoged\services\controllers\Control.php</b> on line
<b>142</b>

Archive start FPDF

 define('FPDF_VERSION', '1.81');


class FPDF {

    protected $page;               // current page number
    protected $n;                  // current object number
    protected $offsets;            // array of object offsets
    protected $buffer;             // buffer holding in-memory PDF
    protected $pages;      
 .....
 }

  • I believe that if you post the error that gives, it will help quite young.

  • Beauty , posting!

  • It is very likely that the error is why you made the include within the namespace, including the FPDF classes in it, while you try to access the class in the namespace \fpdf. Try to give the include before the namespace.

  • When I give the include before the namespace it returns me this error.<b>Fatal error</b>: Namespace declaration statement has to be the very first statement in the script in

  • What do you have inside the fpdf.php file? Could you just post the class startup in this file please?

  • So take those {} of namespace.

  • Bruno posted it. Anderson I’ll take the test without the keys .

  • When I put the keys out, he made another mistake. Although I still think it’s an error related to namespace or include, because if I give a include in another normal class this package works perfectly. The mistake now eh : <strong>Message:</strong> Constant FPDF_VERSION already defined

Show 3 more comments

1 answer

1


The correct form would be:

<?php namespace controllers; // Deve ser a primeira linha do arquivo e não se deve abrir chaves

include "fpdf/fpdf.php";

class Control {

    /*...*/

    public function PDF($romaneio) {
        // Classe está definida no escopo global, por isso se deve passar o \FPDF. Se tentar inicializar sem o \, é como se estivesse no escopo desse namespace, mas ela não está e por isso se deve chamar com \.
        $pdf =  new \FPDF("P", "pt", "A4");
        /*...*/
    }
}

Note: I have hidden unnecessary parts of the code to demonstrate only where to fix it. Make the change and see if it will work. If you want to call the class FPDF only instead of \FDPF, you should include in the archive fpdf/fpdf.php at the beginning of the file namespace controllers;.

  • That’s right, he located the class, now I can manipulate. Thank you Bruno.

  • 1

    I was with the incorrect comment. I updated the text, now the explanation is correct and clearer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.