7

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;
             }
        }

6 Answers 6

22
   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.

Sign up to request clarification or add additional context in comments.

Comments

16

Have your config file create an array of config items. Then include that file in your class's constructor, and save its value as a member variable. This way, all your config settings are available to the class.

test.php:

<?
$config["config_key_security"] = "test";
$config["other_config_key"] = true;
...
?>

test5.php:

<?
class test1 {
    private $config;

    function __construct() {
        include("test.php");
        $this->config = $config;
    }

    public function test2{
        echo $this->config["config_key_security"];
    }
}
?>

2 Comments

This should be the chosen answer
test5.php is best way
8

Another option is to include test.php inside of the test2 method. That will make the variable's scope local to the function.

   class test1 {
            function test2 {
               include('test.php');
               echo $config_key_security;
         }
    }

Still not a good practice though.

2 Comments

So long as it is not abused, this is a very useful way of allowing run-time configuration of classes. It also allows you to separate program logic from presentation by pulling the "template" portion of a function out and placing it in an include.
Worked for me and fastest and simplest. It's for a tiny project (<1K lines) and I only needed to do it in one place.
5

Using __construct() method.

include test.php;
$obj = new test1($config_key_security);
$obj->test2();

class test1
{
    function __construct($config_key_security) {
        $this->config_key_security = $config_key_security;
    }

    function test2() {
        echo $this->config_key_security;
    }
}

Comments

3

the way I prefer to do it is this:

In test.php

define('CONFIG_KEY_SECURITY', 'test');

and then:

in test5.php

include test.php
   class test1 {
            function test2 {
               echo CONFIG_KEY_SECURITY;
         }
    }

Comments

1

You could use the $GLOBALS variable array and put your global variable as element in it.

For example: File: configs.php

<?PHP
    $GLOBALS['config_key_security'] => "test";
?>

File: MyClass.php

<?php
require_once 'configs.php';
class MyClass {
  function test() {
    echo $GLOBALS['config_key_security'];
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.