call function from within the same class

Asked

Viewed 805 times

0

I have a problem with redirecting within the builder of classe.

Following:

I have the 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 the __construct because I have several calls to this class and change everything would be a huge job and a very difficult possible maintenance.

The redirect is being done. But it is duplicating the $_SERVER["SERVER_NAME"].

What to do?

Instead of going out to

http://www.gasmuriae.com.br/manutencao.php

It is redirecting to 

http://www.gasmuriae.com.br/www.gasmuriae.com.br/manutencao.php

  • 2

    You can just call $this-> minhaFuncao();

  • Parse error: syntax error, Unexpected '$this' (T_VARIABLE), expecting Function (T_FUNCTION) or const (T_CONST) in C: Program Files Apache24 Apache24 htdocs gasmuriae.com.br_controlls_util Constants.php on line 181

  • you created a function to redirect to another page ?

  • But you call your function within another cannot get lost in the middle of the class body.

  • yes! exactaemnte

  • $this->minhaFuncao() but will give error, if you want to return this use __construct(). link Veja

  • I think this kind of question quite repeated in the group

  • 1

    Exactly, it was just making the call inside the builder. Thank you!

  • But the redirect failed. Duplicated Location:

  • tries only header("Location: manutencao.php");

  • Can we? Because there are several files in different directories and subdirectories that can call the class. And this file is on the website!

Show 6 more comments

2 answers

1

You have to use the $this->, but not as it is using. A class has a structure, you can only call functions using $this-> on the job.

Class minhaClasse {

      $parametro = true;

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

$mc = new minhaClasse();
$mc->minhaFuncao();
  • I changed the question. See if you can help me with this problem! I can’t declare the class as there are several files that declare it

1

I found:

 $_SERVER["SERVER_PATH"] 

and not

 $_SERVER["SERVER_NAME"]

Thank you to everyone who has worked hard and given their opinions!

Browser other questions tagged

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