class does not load outside of require using autoload

Asked

Viewed 81 times

-2

I have the page below:

<?php 
  require_once '../config.php'; 
  require_once 'config.php'; 
?>
<!doctype html>
<html>
    <head>

        <meta charset='utf-8'>
        <title><?php echo $constantes->getTituloSite(); ?> - Administração - Login</title>

    </head>

    <body>

        <?php 
            if (isset ($_SESSION["login"])) require_once "home.php";
            else {

                 echo '<div id=login>

                            <form action="?" method="post">
                                <input type="hidden" name="logar" />
                                <input type="text" name="login" id="login" required placeholder="Usuário" />
                                <input type="password" name="senha"  id="senha" required placeholder="Senha" />

                                <input type="submit" value="Entrar" />

                            </form>

                        </div>';
            } 



            if (isset($_POST["logar"])) {

                print_r($_POST);

                $administradoresDao = new AdministradoresDao();

                $administradoresDao->buscarTodos ("WHERE login='" . $_POST["login"] . "' AND senha = '" . $_POST["senha"] . "'");

                $administradoresModelos = new AdministradoresModelos();


            }
        ?>

    </body>

</html>

I’m having the following problem:

Note that you have 2 configs required at the outset.

In the middle of the page, notice that I have a

$administradoresDao = new AdministradoresDao();

The first required config.php brings access to the database among other things.

The second brings only:

<?php

    use classes\mvc\modelos\AdministradoresModelos;
    use classes\mvc\modelos\AdministradoresDao;
?>

The problem is this:

When I put the block:

if (isset($_POST["logar"])) {

Inside the page config.php lowly:

<?php

    use classes\mvc\modelos\AdministradoresModelos;
    use classes\mvc\modelos\AdministradoresDao;

    if (isset($_POST["logar"])) {

        print_r($_POST);

        $administradoresDao = new AdministradoresDao();

        $administradoresDao->buscarTodos ("WHERE login='" . $_POST["login"] . "' AND senha = '" . $_POST["senha"] . "'");

        $administradoresModelos = new AdministradoresModelos();


    }; 
?>

Class is normally loaded.

But if I leave where it is or, pasme, put below the require of the same class, it gives that the class was not loaded!

Where is the error?

  • where you do the autoload require?

  • at the root... using Poser. Top so, the config file is found normally because if I put the object inside the Casse loads, but it obviably gives error because I still do not have the form data!

  • so, the use is in the context of the file, I think if you put it inside the require will not roll even

  • yeah, I decided to throw it out and remove the include. However, the previous require has use and works well on the page!

  • where exactly? in the file you put as an example you only prompted two classes...

  • 2 requires de config.php, right? One, the top one, is at the root and also has the use clause. This I can quietly call an object whose use is in it. But the bottom config I can’t. So I had to call the use directly on the body

Show 1 more comment

1 answer

2


In your original code you don’t have the statements use.

In order for you to enter a class, or its full name (namespace + class name) must be used, or you must use the use to use only the class name (no namespace).

TL;DR: Add the statements use in the original code at the beginning of the file.

  • Vinicius. Yes, you do! It’s in the config.php which is the second change in the page that has the 2 uses there!

  • 1

    Trust: https://www.php.net/manualen/language.namespaces.importing.php Statements use are by file. You at least tested the suggestion?

Browser other questions tagged

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