質問

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;

    }
}
役に立ちましたか?

解決

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"
}
*/

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top