0

Im not very knowledgable when it comes to this, but I am learning. I have tried everything I can think of to add a variable to this, so that I dont have to edit this file. I wish to put the API key in a separate config file just to keep things neat and easier to make changes rather than having to remember all the files that have to be edited for these api keys. Here is the code I cannot figure out.

private $_nodes = array("https://omdbapi.com/?apikey=123456&i=%s");

I tried this, but no joy. Cant figure it out. I tried many variations. Maybe this isnt even possible in an array, like I said, I have alot to learn

private $_nodes = array("https://omdbapi.com/?apikey='"$site_config['OMDBAPIKEY']"'&i=%s");

I have managed to move all other API keys to the config, but this one just wont work

4
  • 4
    . is the concatenation operator in PHP. Commented Mar 15, 2018 at 22:54
  • Wait, it looks like you're initializing an object property. I don't think that will work with an array key even after you fix the syntax error. Commented Mar 15, 2018 at 22:57
  • 1
    I don't think you should be playing with APIs if you don't even know how to concatenate strings in the language you're using. Commented Mar 15, 2018 at 23:03
  • It was a typo. I was quickly trying to copy paste the code and I backed a few characters out when I hit cntrl-z a few too many times Commented Mar 16, 2018 at 4:40

3 Answers 3

2

I think I see two problems.

One certainly is a syntax error. You need to use the . operator to join the strings together.

private $_nodes = array("https://omdbapi.com/?apikey='" . $site_config['OMDBAPIKEY'] . "'&i=%s");

But the private implies this is initializing an object property. Using your array value there will cause

Fatal error: Constant expression contains invalid operations

If you want to assign it using that value, you can do it in the constructor instead.

private $_nodes;

function __construct()
{
    $this->_nodes = array("https://omdbapi.com/?apikey='". $site_config['OMDBAPIKEY'] . "'&i=%s");
}

($site_config is undefined in this example, but I don't really know where it comes from. You'll have to define it in the scope of the constructor somehow. Maybe pass it as a parameter.)

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

Comments

0

As Don't Panic's answer says, the first problem you have is that you have to concatenate (join) the strings together using the concatenation . operator.

As that answer says, you also can't define an object property with the value of a variable in a class definition.

You can, however, use a constant value. So you would need to first of all change $site_config to be a const rather than a variable:

const Site_Config = array(
    'OMDBAPIKEY' => 123456,
    /* other definitions */
);

class SomeUsefulClass {
    private $_nodes = array("https://omdbapi.com/?apikey='" . Site_Config['OMDBAPIKEY'] . "'&i=%s")
}

Whether this is a good idea or not is decidedly open to question...

Comments

0

The problem with your code is the concatenation of strings. In PHP you can do it in several ways

As of PHP 5.6.0 it's possibel to use scalar expression involving numeric and string literals and/or constants in context of a class constant.

It means that it's possible to do following

const KEY ='OMDBAPIKEY';
private $_nodes = array("https://omdbapi.com/?apikey={self::KEY}&i=%s");

But it's not the cleanest way. The better option is to add it to constructor f.e you can see different types of concatenation in following code

<?php
class Test {
    private $_nodes = [];

    public function __construct(array $site_config)
    {
        // simple concatenation with .
        $this->_nodes[] = "https://omdbapi.com/?apikey=". $site_config['OMDBAPIKEY'] ."&i=%s";
        // using sprintf my prefered way, noticed escaped %s with i=
        $this->_nodes[] = sprintf("https://omdbapi.com/?apikey=%s&i=%%s", $site_config['OMDBAPIKEY']);
        // using complex curly syntax
        $this->_nodes[] = "https://omdbapi.com/?apikey={$site_config['OMDBAPIKEY']}&i=%s";
    }

    public function printNodes()
    {
        var_dump($this->_nodes);
    }
}

$t = new Test(['OMDBAPIKEY' => 'abc']);
$t->printNodes();

It will print

array(3) {
  [0]=>
  string(36) "https://omdbapi.com/?apikey=abc&i=%s"
  [1]=>
  string(36) "https://omdbapi.com/?apikey=abc&i=%s"
  [2]=>
  string(36) "https://omdbapi.com/?apikey=abc&i=%s"
}

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.