call function from within the same class

0

I have a redirect problem within the constructor of classe .

Next:

I have classe :

<?php
 class Constantes {
     
    private $livre = false;
    ...

    public function __construct () {    

       if($this->livre == false) {
           header("Location: ".$_SERVER["SERVER_NAME"]."/manutencao.php");
           exit;
       }
      
      .....

 }
?>

Well, it is necessary that the redirection be done there in __construct because I have several calls to this class and changing everything would be a huge job and a possible very difficult maintenance.

Redirection is being done. But you are duplicating the $ _ SERVER ["SERVER_NAME"] .

What to do?

Instead of leaving for

link

You are redirecting to

link

    
asked by anonymous 14.11.2017 / 18:13

2 answers

1

You have to use $this-> , but not how you are using it. A class has a structure, you can only call functions using $this-> within functions.

Class minhaClasse {

      $parametro = true;

      public function minhaFuncao () {
          if ($this->parametro == false) header("Location: pagina.php");
      }
  }

$mc = new minhaClasse();
$mc->minhaFuncao();
    
14.11.2017 / 18:18
1

I found:

 $_SERVER["SERVER_PATH"] 

and not

 $_SERVER["SERVER_NAME"]

Thank you to everyone who tried hard and gave their opinions!

    
14.11.2017 / 20:21