Global variable in all class functions

2

Normally when I want to pull a variable that is outside the class, I use global $variavel , but I wanted this variable to be accessible in all functions of the class, so I do not need to "pull" all the functions, how?

    
asked by anonymous 10.03.2015 / 20:26

3 answers

5

My understanding of the question is that it is necessary to save the connection data in a variable that can be accessed from anywhere in the application. A global variable is a solution to this but in fact it is not good to use it.

So the solution is to encapsulate the non-global variable into a function by maintaining its static state or using an approach considered more modern, but not necessarily better, by encapsulating the variable in a static class.

Note that being static is the secret why this gives a life time for the variable equal to the time of the application, that is, the variable is available all the time just as the global variable is but has the advantage of the non- be directly exposed by creating ambiguities and conflicts with other local variables. It is best to have global functions or classes. Even this may not be the ideal solution, some would criticize this solution that is pragmatic.

class Conexao {
    private static $conexao = "dados da conexao aqui - classe";
    public static function PegaConexao() {
        return self::$conexao;
    }
}

function conexao(){
    static $conexao = "dados da conexao aqui - funcao";
    return $conexao;
}

class Uso {
    public function AbreBanco1() {
        echo conexao() . "\n";
    }
    public function AbreBanco2() {
        echo Conexao::PegaConexao();
    }
}

Uso::AbreBanco1(); //chama só para demonstrar
Uso::AbreBanco2(); //chama só para demonstrar

See running on ideone .

As the question does not give details I can only show a generic solution but it is easy to adapt to use according to the specific need.

It does not have to have both ways, I put both to exemplify, obviously only a AbreBanco() would exist. And the contents of the variable will probably be different.

    
11.03.2015 / 01:00
3

Create accessible variable for all functions of a class:

<?php

    class teste {

        /* construct */
        function __construct($variavel) {

            $this->global_variavel  = $variavel;

        }

        function checando_funcao() {

                echo $this->global_variavel;

        }

        function checando_funcao2() {

                echo $this->global_variavel;

        }

?>
  • To run a test:

        //Criando uma nova instância
    
        $variavel = 'Meu texto!';
        $a = new teste($variavel);
    
        //Executando as duas funções com os mesmos valores de uma única variável "global"
        $a->checando();  
        $a->checando_funcao2();
    

Note:

  

To ensure reverse compatibility, if PHP 5 can not find a __construct () for a given class, it will look for the old-fashioned constructor function, which has the same class name. Effectively, it means that the only case that can cause compatibility problems would be if the class has a method called __construct () that is used for another purpose that does not initialize the object.

    
10.03.2015 / 20:32
1

To make a variable member of the class set the access modifier and then its name

public - is accessible by all methods of the class and outside of it as well.

protected - accessible only within the class and its derivatives.

private - accessible only to the methods of the class.

class Teste{
   public $variavel = 'esse valor pode ser modificado externamente';

   public function foo(){
     echo $this->variavel;
   }
}
    
10.03.2015 / 20:31