include php does not work

Asked

Viewed 2,504 times

0

I have the following directory tree:

.var
..|--www
......|--site
..........|--index.php
..........|--all
..............|--controller
..................|--conexao.php (classe)
..............|--model
..................|--bo
..................|--vo
..................|--dao
.....................|--prioridade_dao.php (classe)

Pretty extensive beauty, the problem is this: It includes the priority class_dao.php inside index.php (so far so good) if I "NO" put no include in the prioritity_dao.php appear things when viewing the page, now if I put any include in the priority class_dao.php does not appear anything else. I’ve tried to:

include ('../../controller/conexao.php');
include '../../controller/conexao.php';
include ("../../controller/conexao.php");
include "../../controller/conexao.php";
include $_SERVER['DOCUMENT_ROOT']."/all/controller/conexao.php";

Follows priority class_dao.php:

<?php
Class PrioridadeDao{
    error_reporting(-1); 
    ini_set('display_errors', 'On');

    include $_SERVER['DOCUMENT_ROOT']."/all/controller/conexao.php";
    $conexao = new Conexao();       

    public function __construct(){

    }

    public function teste(){
        echo "Chega aqui sim";
    }


    public function selectAll(){
        echo "<br />CHEGA AQUI?";

        if(!$conexao->conecta()){
            exit;
        } else {
            $result = pg_query($conexao->conecta(), "SELECT * FROM prioridade");
            while ($consulta = @pg_fetch_array($result)){
                echo $consulta['nivel']." ".$consulta['nome'];
            }
            $conexaoObj->encerra($conexao->conecta());
        }
    }
}
?>

I have also tried with require, all without success. I did a test calling for direct include to conexao.php in index.php and I did it. I’m using methods __construct() empty in the classes, can that be it? Finally I have researched in a lot of places, tested many things and nothing :( if you can help me I will be very grateful.

  • After all you want to include which file within which file?

  • 1

    Show priority code_dao.php

  • enables errors in php.ini and puts the error here

  • I want to include the.php connection within the priority_dao.php

  • Place this at the beginning of the priority file_dao.php: error_reporting(-1);&#xA;ini_set('display_errors', 'On');

  • Use absolute Path instead of relative Path.

  • @Luangabrieldacostarodrigue I edited my answer.

  • Put include inside some method, or outside the class

  • Fabio changed the post. $_SERVER['DOCUMENT_ROOT'] prints this exactly: /var/www/ticketdbx/public_html right where I needed it.

  • @Luangabrieldacostarodrigue to notify the user, put a@ in front of his name. And use the answer space only to answer questions. I converted your comment here :)

Show 5 more comments

3 answers

3

Correct in working with OPP, is to set your settings files in a folder called Config, the directory Controller, as its name already says is to send and receive commands to the view or model. And your connection file is in totally wrong place.

Includes with paths relative are problematic

require_once '../../controller/conexao.php'

Use absolute paths, i.e., include the full path of the file, a simple way to do this, is to define this value in a constant and be called by a Bootstrap.

define('CONFIG', dirname(dirname(__FILE__)). 'Config'. DIRECTORY_SEPARATOR);

(PHP 5 => v5.3)

define('CONFIG', dirname(dirname(__DIR__)). 'Config'. DIRECTORY_SEPARATOR);

Then just include the file where you want it.

require_once CONFIG . 'conexao.php';
  • Hi Mandrake, thanks for the tip, but for those who do not know fully the language (I’m starting in PHP) your tip is flying in the air, I do not know how to use the function defines or how to make this constant call with bootstrap, in my case does not help anything :( but it was worth it anyway.

  • Place this constant in the index, and an echo, when finding the script path, do the required in the class, each dirname applied, will go back a root directory, this way you find the script path, learn more on manual. and constant is basic language ;)

1


Put your include out of class, so:

<?php
error_reporting(-1); 
ini_set('display_errors', 'On');

$ds = DIRECTORY_SEPARATOR;

require_once __DIR__."{$ds}..{$ds}..{$ds}controller{$ds}conexao.php";

Class PrioridadeDao{

    private $conexao;

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

    public function teste(){
        echo "Chega aqui sim";
    }


    public function selectAll(){
        echo "<br />CHEGA AQUI?";

        if(!$conexao->conecta()){
            exit;
        } else {
            $result = pg_query($conexao->conecta(), "SELECT * FROM prioridade");
            while ($consulta = @pg_fetch_array($result)){
                echo $consulta['nivel']." ".$consulta['nome'];
            }
            $conexaoObj->encerra($conexao->conecta());
        }
    }
}

The scope of a class must be to create properties (context variables) and methods (class functions). Class requests that will be used by the class in question must be inserted before the class declaration.

  • Opa in this way the errors began to appear and something already went ahead. Now I see another problem, I can’t instantiate the class connected.php if I instate it in the constructor later there is error in the method, and if I instate outside the constructor simply does not appear anything on the page.

  • I did an update @Luangabrieldacostarodrigue

  • @Fabio Cardoso, I managed to make it work by following the tips that Kaduamaral provided. Using include outside the class and instantiating in the constructor method. Even so I would like to thank everyone who has made available their time and willingness to help me. Thank you all.

  • @Luangabrieldacostarodrigue I only provided details to fix the current problem, to avoid these types of problem the best thing to do is to follow patterns. Give a good study in MVC patterns and try to develop according to response by Mandrake. Hugs and success.

0

If you want to include conexao.php inside prioridade_dao.php would be this way:

require_once '../../controller/conexao.php';

*If unable to include will give error and will be exposed error.

The error in your code is that you are making the inclusion within the test function but it is not called so there is no inclusion (try to have a simpler architecture because it minimizes the chances of getting out of control). make the inclusion directly below the <?php the way I did above.

  • @Luangabrieldacostarodrigue if there is error post here.

  • I did this, the output I put inside the function was to see what would be the printed directory to see if it would be correct.

  • @Luangabrieldacostarodrigue the way I did what was reported?

  • I managed to solve by doing the way Kadu presented using include outside the class.

Browser other questions tagged

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