include php does not work

0

I have the following tree of directories:

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

Beauty quite extensive, the problem is as follows: It includes the class_of.php priority within index.php (so far so good) if I "NO" put any include in the priority_of.php appear things when viewing the page, now if I put any include in the class priority_dao.php does not appear nothing more. Already tried:

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

Next class class.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 to no avail. I made a test calling by direct include the conexao.php in the index.php and I was able to do it. I'm using __construct() empty methods in classes, can this be? Anyway I have already researched a lot of places, tested many things and nothing :( if you can help me I will be very grateful.

    
asked by anonymous 13.07.2015 / 15:15

3 answers

1

Put your include outside the class, like this:

<?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 entered before the class declaration.

    
13.07.2015 / 15:39
3

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

Includes with% relative% are problematic

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

Use absolute paths, ie include the full path of the file, a simple way to do this is to set this value to a constant and be called by a paths .

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';
    
13.07.2015 / 15:52
0

If you want to include conexao.php within prioridade_dao.php it would look like this:

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

* If you can not include it will give error and the error will be exposed.

The error in your code is that you are doing the inclusion inside the test function but it is not called as soon as there is no inclusion (try to have a simpler architecture because it minimizes the chances of getting out of control). make the direct inclusion just below <?php as I did above.

    
13.07.2015 / 15:24