0

I have a settings.php file that looks something like this:

$user="adam";
$pass="something";

then I have a class called funcs.php, in which I have a require "../settings.php" on top, but I don't know how to get the scope of "$user" into my methods...

I'm sure this is an easy fix, but I've been googling in circles for a while with no luck.

My class looks something like this:

<?php
require_once __DIR__."/../settings.php";

class Funcs
{

    public function something()
    {
        // do stuff
return $user;

    }
}
1
  • See my answer here and my blogpost here, use a configuration container and insert that into your methods. Commented May 3, 2014 at 1:29

2 Answers 2

2

you can use the return construct inside file which can return anything, but if you want to return multiple values, it's best to use an array.

settings.php

return array(
  'user' => 'adam',
  'pass' => 'something'
);

file.php

$settings = require __DIR__.'settings.php';

echo $settings['user']; // adam

To pass everything returned from the file into a class:

class MyClass {
  public function __construct(array $array) {
    foreach($array as $key => $value) {
      $this->{$key} = $value;
    }
  }
}

$class = new MyClass(require __DIR__ . '/settings.php');
var_dump($class);
/*
 class MyClass#1 (2) {
  public $user =>
  string(4) "adam"
  public $pass =>
  string(9) "something"
}
*/
Sign up to request clarification or add additional context in comments.

9 Comments

Isn't it possible to declare a variable in the class, set its value to the $user from settings, and then refer to it as $this->variable_name? (I just dont remember how to do it...)
Yes. Tell me the class name and I can show you how.
settings.php - the file with all the variables. Funcs.php - the class.
@Ryan take a look at what I provided, and let me know if that is what you mean.
Not really, because you are passing it in the constructor... I thought by having an include in the top of the class file I could avoid having a constructor in the class
|
2

I think the issue is that these are global variables used inside a function or class scope.

If you want to access these from within a function scope you could use the "global" keyword, for example:

public function foo() {
    global $user;
    echo $user;
}

However – using global (and global variables in general) is usually considered pretty smelly these days.

Some alternatives:

Pass the configuration into the constructor of your class, therefore:

$config = array(
    'user' => 'adam',
    'pass' => 'something'
);

class MyConsumer {
    private $config;

    public function __construct($config) {
        $this->config = $config;
    }
}

$instance = new MyConsumer($config);

This is usually termed "dependency injection", because your config can be easily substituted for something else depending on the context (such as in unit tests).

An alternative is to have a singleton class which encapsulates your config, which you'd access something like (not tested but you get the idea):

// production_config.php
return array(
    'user' => 'adam',
    'password' => 'something'
);
// config.php
class Config {
    protected $config;
    protected static $instance = null;

    protected function __construct() {
        $this->config = require('production_config.php');
    }

    protected function getKey($key) {
        if(isset($this->config[$key])) {
            return $this->config[$key];     
        }
        // Possibly throw exception or something else if not found
    }

    public static function get($key) {
        if(self::$instance === null) {
            self::$instance = new self();       
        }
        return self::$instance->getKey($key);
    }

}

So you can go:

$user = Config::get('user');

This is perhaps easier than passing your config into every class, but it's more difficult to substitute your config for testing purposes.

7 Comments

Where is Config defined?
Yep, I don't want to use Globals nor do I want to pass it via the constructor as what I wrote above was just an example, I have a lot of variables in that file that I want to use in my classes...
In another class, typically you'd "include" it to your global scope either when you bootstrap the application (if you have a central entry point) or by including it in your global includes on every page.
I'll add an example of a config singleton to my answer.
Thanks for taking the time to write all that, but I have never worked with singletons, and I think I have some reading to do!
|

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.