class does not open

Asked

Viewed 102 times

0

My structure

localhost/sistema
localhost/sistema/.htaccess
localhost/sistema/composer.json
localhost/sistema/composer.lock
localhost/sistema/composer.phar
localhost/sistema/App
localhost/sistema/App/mvc/
localhost/sistema/App/mvc/Controles
localhost/sistema/App/mvc/Controles/Planos.php
localhost/sistema/App/mvc/Modelos
localhost/sistema/App/mvc/Modelos/Planos.php
localhost/sistema/App/mvc/Vistas
localhost/sistema/App/mvc/Vistas/Conexao.php
localhost/sistema/App/mvc/Vistas/Planos.php
localhost/sistema/vendor
localhost/sistema/public
localhost/sistema/public/index.php

Plans.php Controls

<?php 

 namespace CONTROLES;
 use MODELOS\Planos;

 class Planos {

     private $conexao;

     public function __construct ($_conexao) {  
         $this->conexao = $_conexao;
     }

     public function alteraPlano ($plano) {

         $string = "UPDATE planos 
                    SET nome ='".$plano->getNome()."', 
                        descricao = '".$plano->getDescricao()."' 
                    WHERE idPlano = ".$plano->getIdPlano();

         return $this->conexao->query($string);
     }


     public function excluir ($idPlano) {        
         // CHAVE ESTRANGEIRA E ON DELETE CASCADE ATIVADO NO BANCO
         $string = "DELETE FROM planos WHERE idPlano = ".$idPlano;

/*       
        DA FORMA ABAIXO É SOMENTE QUANDO NÃO CONFIGUROU O DELETE CASCADE NO BANCO
         $string = "DELETE planos, fotos FROM planos
                    LEFT JOIN fotos  ON 
                        planos.idPlano = fotos.idPlano
                    WHERE planos.idPlano = ".$idPlano;
*/                  
         return $this->conexao->query($string);

     }

     public function cadastrar ($plano) {        

         $string = "INSERT INTO planos (                                          
                      nome,
                      descricao
                     ) 
                     VALUES (
                       '".$plano->getNome()."',
                       '".$plano->getDescricao()."'
                       )
                       ";

         return $this->conexao->query($string);

     }

     public function ultimoIdCadastrado () {         
         return $this->conexao->insert_id;
     }

     public function pesquisaPlano($idPlano) {

         $plano = null;           

         $string = "SELECT 
                       idPlano,
                       nome,
                       descricao
                    FROM planos 
                    WHERE idPlano = ".$idPlano;

         $registros = $this->conexao->query($string);
         $quantasLinhas = $registros->num_rows;      

         if ($quantasLinhas > 0) {

             list ($idPlano, $nome, $descricao) = $registros->fetch_row();

             $plano = new Planos($nome, $descricao);             

             $plano->setIdPlano($idPlano);
         }

         return $plano;

     }



     public function pesquisaPlanos($where = NULL) {


         $planos = null;           

         $string = "SELECT idPlano, nome, descricao FROM planos ".$where;

         $registros = $this->conexao->query($string);
         $quantasLinhas = $registros->num_rows;      

         if ($quantasLinhas > 0) {

             while (list ($idPlano, $nome, $descricao) = $registros->fetch_row()) {

               $plano = new Planos($nome, $descricao);                       
               $plano->setIdPlano($idPlano);

               $planos[] = $plano;

             }
         }

         return $planos;
     }

 }
?>

Planos.php Modelos

<?php

  namespace MODELOS;

  class Planos {

      private $idPlano;
      private $nome;
      private $descricao;

      public function __construct (
          $_nome, 
          $_descricao) {

          $this->nome = $_nome;
          $this->descricao = $_descricao;
      }   

      public function setIdPlano ($_idPlano) {
          $this->idPlano = $_idPlano;
      }

      public function getIdPlano () {
          return $this->idPlano;
      }

      public function getNome () {
          return $this->nome;
      }
      public function getDescricao() {
          return $this->descricao;
      }

  }

?>

Planos.php Vistas

<?php

   namespace VISTAS;

   use CONTROLES\Planos as PC;
   use MODELOS\Planos as PM;

   class Planos {

       private $conexao;

       public  function __construct ($_conexao) {
           $this->conexao = $_conexao;
       }

       public function getPlanos () {

           $planos = new PC($this->conexao);

           $todos = $planos->pesquisaPlanos();

           foreach ($todos as $plano) :
              echo $plano->getNome()."<br />";
           endforeach;

       }


   }

index.php public

<?php 

  ini_set("display_errors",true);
  ini_set("display_startup_erros",1);
  error_reporting(E_ALL && E_NOTICE && E_STRICT);

  require_once "../App/mvc/Vistas/Conexao.php";
  require_once "../vendor/autoload.php";

  $conecta = new Conexao();  
  $conexao = $conecta->abreConexao();

  $planos = new Planos($conexao);   
  print_r($planos->getPlanos());


?>

and Composer.json

{    
  "require" : {
        "php" : "^5.5 || ^7.0",
        "ext-mbstring": "*"
    },
  "authors": [
        {
            "name": "Carlcleo",
            "email": "[email protected]",
            "role": "Desenvolvedor"
        }],
   "autoload": {
      "classmap": ["App"]
   }
}

When I spin index.php of server error.

Fatal error: Uncaught Error: Class 'Planos' not found in C:\Program Files\Apache24\Apache24\htdocs\mvc\public\index.php:14 Stack trace: #0 {main} thrown in C:\Program Files\Apache24\Apache24\htdocs\mvc\public\index.php on line 14

Where am I going wrong?

Can accompany in:

http://funerariasaopedro.net.br/mvc

Adding autoload_classmap:

<?php

// autoload_classmap.php @generated by Composer

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

return array(
    'CONTROLES\\Planos' => $baseDir . '/App/mvc/Controles/Planos.php',
    'Conexao' => $baseDir . '/App/mvc/Vistas/Conexao.php',
    'MODELOS\\Planos' => $baseDir . '/App/mvc/Modelos/Planos.php',
    'VISTAS\\Planos' => $baseDir . '/App/mvc/Vistas/Planos.php',
);
  • The class is in a namespace that you did not put in calling it. And if you’re using Autopload from Poser, you don’t have to import the files manually.

  • Ok, after the fix, the error went to the Plans.php class that is in the Views folder that says Fatal error: Uncaught Error: Class 'CONTROLS Plans' not found in C:. Note: this class is now starting like this: <?php namespace VISTAS; use PC Flat CONTROLS; use PM Flat TEMPLATES;

  • Check the Namespaces in uppercase, because php is case sensitive. FLAT TEMPLATES is different from flat templates, see the paths, because if there is a folder named minuscule there could be problems in case of linux use.

  • is all hitting correctly. That’s the problem. Adding autoload_classmap

  • Spun composer dump?

  • yes. same way. I added autoload_classmap at the end of the question

Show 1 more comment

1 answer

2

Your namespace is VISTAS, so you have to use the use in the index.php also:

<?php 

ini_set("display_errors",true);
error_reporting(E_ALL|E_STRICT);

use VISTAS\Planos;

require_once "../App/mvc/Vistas/Conexao.php";
require_once "../vendor/autoload.php";

$conecta = new Conexao();
$conexao = $conecta->abreConexao();

$planos = new Planos($conexao);
print_r($planos->getPlanos());

In other words, in each document the use or point to the full namespace:

<?php 

ini_set("display_errors",true);
error_reporting(E_ALL|E_STRICT);

require_once "../App/mvc/Vistas/Conexao.php";
require_once "../vendor/autoload.php";

$conecta = new Conexao();
$conexao = $conecta->abreConexao();

$planos = new VISTAS\Planos($conexao);
print_r($planos->getPlanos());

For the record, the use of and is somewhat "wrong":

ini_set("display_startup_erros",1);
error_reporting(E_ALL && E_NOTICE && E_STRICT);

As I explained in your other question I sent you:

Simply setting up in php.ini would already solve it, if it’s production then don’t use it, just for development, besides display_startup_erros really has a different intention than you are probably imagining and applying error_reporting will have no effect, apart from the purpose of the error_reporting is much more than displaying errors, read this:

The use of && in the error_reporting this wrong.

  • Hi, thanks for the remarks. I’ll stay tuned. When asked, I noticed the following: The namespaces are working correctly now, but the access to the mysql query function is not returning value although the table is populated. What can it be?

  • @Carlosrocha depends, doesn’t it work like? Syntax error? I think this is another problem and should be another question.

  • I found: sorry, it was error in connection class.

Browser other questions tagged

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