class does not open

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

Planos.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;
      }

  }

?>

Plans.php Views

<?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 start% s 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?

You can keep track of:

link

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',
);
    
asked by anonymous 12.06.2018 / 23:21

1 answer

2

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

<?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());

That is, in each document you have to indicate the use or 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());

Just to state, the use of and this is somewhat "wrong":

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

As I already explained in your other question I sent you:

Simply setting up in php.ini would already solve, if it is production then do not even use it, just for development, other than display_startup_erros actually has a different intent than you are probably imagining and applying error_reporting will have no effect, since the intent of error_reporting is much more than displaying errors, read this:

The use of && in error_reporting is wrong.

    
13.06.2018 / 18:55