0

I'm struggling with a "noob question" in PHP : I would have a conf.php file containing something like that :

<?php

static $oauthConfig = array(
    'facebook'=> array(
        'appId'     => 'xxx' ,
        'secret'    => 'xxx' ,
        'loginURL'  => 'xxx' ,
        'logoutURL' => 'xxx'
    ) ,
    'twitter' => array(
        'appId'     => 'xxx' ,
        'secret'    => 'xxx' ,
        'loginURL'  => 'xxx' ,
        'logoutURL' => 'xxx'
    )
);

?>

I need an array in order to add some nested levels to it, and keep it simple to understand and access, with something like :

//auth.php , in the same folder than conf.php

<?php

require_once( 'conf.php' );

$service = $_REQUEST[ 'ref' ];

switch( $service )
{
    case 'facebook':
    {
        $params = $oauthConfig[$service];
        $fb     = OAuthFactory::getInstanceOf( $service , $params );
                    ...
        break;
    }
}

...
?>

I don't figure how to use my conf.php file, since including it does not allow me to use the array. I tried some configurations (with/out static, etc.), but no way to get it work...

Unfortunately, parsing the Google Wisdom did not help me a lot for that.

I would avoid using .ini files or XML conf.

Benj
  • 1,184
  • 7
  • 26
  • 57

3 Answers3

2

You seem to be experiencing a scoping error, or the library you're using expects objects instead of arrays. Regardless of that problem, consider using INI files, despite your aversion to them. They're simple, predictable, and malleable. For example, if this was your conf.ini:

; comments
[facebook]
appId = 1231456465798
loginURL = "http://www.somewhere/login"

; more comments
[twitter]
appId = 3432423342

You could read it with $conf = parse_ini_file( "conf.ini", true );

In this form, print_r( $conf ); shows:

Array (
    [facebook] => Array (
            [appId] => 1231456465798
            [loginURL] => http://www.somewhere/login
        )
    [twitter] => Array (
            [appId] => 3432423342
        ))

If need be, you could convert this into a stdObject for your receiving library:

$params = (object)$conf['facebook']; var_dump( $params );

object(stdClass)#1 (2) {
  ["appId"]=> string(13) "1231456465798"
  ["loginURL"]=> string(26) "http://www.somewhere/login"
}
pp19dd
  • 3,625
  • 2
  • 16
  • 21
1

I think what you are looking for is a global array.

global $oauthConfig;
$oauthConfig = array(
'facebook'=> array(
    'appId'     => 'xxx' ,
    'secret'    => 'xxx' ,
    'loginURL'  => 'xxx' ,
    'logoutURL' => 'xxx'
) ,
'twitter' => array(
    'appId'     => 'xxx' ,
    'secret'    => 'xxx' ,
    'loginURL'  => 'xxx' ,
    'logoutURL' => 'xxx'
)
);
Karan Punamiya
  • 8,603
  • 1
  • 26
  • 26
  • Does not the trick... but why ? – Benj Dec 03 '12 at 15:47
  • 1
    try to pinpoint the location where it doesnt do the trick, start by outputting the values in the array by print_r immediately after the include/require (and maybe nl2br for prettyness). Use require for an error message when the file is missing. – Adder Dec 03 '12 at 15:50
  • The most likely reason why you can't access this global array is because you are not passing it into functions and classes correctly. Wherever you want to access this array you must use the global keyword or the array will be out of scope and therefore inaccessible. – EmmanuelG Dec 03 '12 at 15:53
  • @Adder : I added var_export($oauthConfig) at the end of the conf.php file, it shows the contents of the array, no problem. Thanks for nl2br, I wasn't knowing about it :) – Benj Dec 03 '12 at 15:57
  • @EmmanuelG : I'm not using classes in this part of the code, all is "raw" in files. – Benj Dec 03 '12 at 15:57
  • 2
    Where are you trying to access the array for use? You define the values in your conf file, but where are you using the array? If you define the variable in conf.php and include conf.php in a file then your array will come along for the ride. However, if you are trying to access the array's contents from within a function, you must either pass it as an argument to the function or access it by using global $oauthConfig; from within your function. See [global variables in php not working as expected](http://stackoverflow.com/questions/107693/global-variables-in-php-not-working-as-expected) – EmmanuelG Dec 03 '12 at 16:03
  • @Karan Punamiya : I found my mistake using your solution, was a stupid typo in the name following global declaration. It works now. Thanks ! +1 – Benj Dec 03 '12 at 16:04
  • @EmmanuelG : thanks for all these details, I write them in my little brain. +1 – Benj Dec 03 '12 at 16:05
0

You already have the array declaration in conf.php! you just have to include it and use it normally:

include "conf.php";

$params=$oauthConfig[$service];
Naryl
  • 1,878
  • 1
  • 10
  • 12