Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Include Variable inside Class in php

Tags:

php

i have some file test.php

<?PHP
    $config_key_security = "test";
?>

and i have some class

test5.php

 include test.php
       class test1 {
                function test2 {
                   echo $config_key_security;
             }
        }
like image 811
monkey_boys Avatar asked Nov 27 '22 21:11

monkey_boys


1 Answers

   class test1 {
            function test2 {
               global $config_key_security;
               echo $config_key_security;
         }
    }

or

   class test1 {
            function test2 {
               echo $GLOBALS['config_key_security'];
         }
    }

Having your class rely on a global variable isn't really best practice - you should consider passing it in to the constructor instead.

like image 61
Greg Avatar answered Jan 05 '23 18:01

Greg