0

I'm trying to include a php configuration file to my main, index.php file, but I faced two problems:

1) I don't know how to properly build an array and output a desirable "value" of the "key". For example:

function config() {
  return $settings = array(
    'url' => 'https://example.com',
    'title' = > 'My dynamic website'
  }
}

Is the above code correct? How do I echo the url, for example(or what is the best way to output it)?

2) I also thought of making a function to output the desirable value, but I doubt it's the right way. I tried this and it worked:

function myWebsiteURL() {
  echo 'https://example.com'
}

And then I just called this function when needed.

What is the right way to make this kind of things, in terms of security and performance etc... I'm really just starting out, but I want to code in a right way from the beginning.

  • You have a few typos in the first function. Here's how you could echo the URL, https://3v4l.org/SDlFC. If you were running that first code block and didn't get errors you should enable error reporting, see https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display. – chris85 Apr 11 '18 at 18:54
  • You're right, Chris! I missed some `)` :) Also, thanks for showing me how this should work. but could you explain why does `echo config(['url']);` won't work, even if I removed the `$config = config();`? – kirewuxivo Apr 11 '18 at 19:07
  • _"why does echo config(['url']); won't work"_ - because you are just calling a function with a parameter here (that it doesn't even care about.) `([])` and `()[]` are two completely different things. – CBroe Apr 11 '18 at 19:12
  • Yes, thank you CBroe, Progrock told me how to make this right – kirewuxivo Apr 11 '18 at 19:17

2 Answers2

1

Personally, I would implement ArrayAccess, Countable and use ArrayObject for storage, then do additional stuff which will allow full flexibility over the class including values can be accessed as arrays or as an object, counting, plus additional sugar like outputting as json (perhaps for js), or clean print_r's for debugging.

<?php
class Config implements ArrayAccess, Countable
{
    /**
     * @var ArrayObject
     */
    private $storage;

    /**
     * @param array $preset
     */
    public function __construct($preset)
    {
        $this->storage = new ArrayObject($preset);
        $this->storage->setFlags(
            ArrayObject::STD_PROP_LIST | ArrayObject::ARRAY_AS_PROPS
        );
    }

    /**
     * ArrayAccess offsetGet (getter).
     *
     * @param string $index
     */
    public function offsetGet($index)
    {
        return isset($this->storage->{$index}) ? $this->storage->{$index} : null;
    }

    /**
     * ArrayAccess offsetSet (setter).
     *
     * @param string $index
     * @param mixed  $value
     */
    public function offsetSet($index, $value)
    {
        if (is_null($index)) {
            $this->storage[] = $value;
        } else {
            $this->storage->{$index} = $value;
        }
    }

    /**
     * ArrayAccess offsetExists (isset).
     *
     * @param string $index
     */
    public function offsetExists($index)
    {
        return isset($this->storage->{$index});
    }

    /**
     * ArrayAccess offsetUnset (unset).
     *
     * @param string $index
     */
    public function offsetUnset($index)
    {
        unset($this->storage->{$index});
    }

    /**
     * Magic method (getter).
     *
     * @param string $index
     */
    public function __get($index)
    {
        return $this->offsetGet($index);
    }

    /**
     * Magic method (setter).
     *
     * @param string $index
     * @param mixed  $value
     */
    public function __set($index, $value)
    {
        return $this->offsetSet($index, $value);
    }

    /**
     * Magic method (isset).
     *
     * @param string $index
     */
    public function __isset($index)
    {
        return $this->offsetExists($index);
    }

    /**
     * Magic method (unset).
     *
     * @param string $index
     */
    public function __unset($index)
    {
        return $this->offsetUnset($index);
    }

    /**
     * Magic method (as function invoker).
     *
     * @param mixed  $arguments
     */
    public function __invoke(...$arguments)
    {
        if (isset($this->storage->{$arguments[0]})) {
            return $this->storage->{$arguments[0]};
        }
    }

    /**
     * Magic method (toString well json).
     */
    public function __toString()
    {
        $return = [];
        foreach ($this->storage as $key => $value) {
            $return[$key] = $value;
        }
        return json_encode($return, JSON_PRETTY_PRINT);
    }

    /**
     * Magic method (override print_r/var_dump).
     */
    public function __debugInfo()
    {
        $return = [];
        foreach ($this->storage as $key => $value) {
            $return[$key] = $value;
        }
        return $return;
    }
    /**
     * Implements Countable
     */
    public function count()
    {
        return $this->storage->count();
    }
}


$config = new Config([
    'url' => 'https://example.com',
    'title' => 'My dynamic website'
]);

echo $config->url.PHP_EOL;
echo $config['title'].PHP_EOL;
echo count($config).PHP_EOL;
echo print_r($config, true).PHP_EOL;
echo $config.PHP_EOL;

https://3v4l.org/Cfd4t

Result:

https://example.com
My dynamic website
2
Config Object
(
    [url] => https://example.com
    [title] => My dynamic website
)

{
    "url": "https:\/\/example.com",
    "title": "My dynamic website"
}
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
0

You have some syntax errors (corrected ahead):

<?php

function config() {
  return $settings = array(
    'url' => 'https://example.com',
    'title' => 'My dynamic website'
  );
}

echo config()['url'];

Output:

https://example.com

There's no need for the assignment to the variable $settings, you can just return an array.

Progrock
  • 7,373
  • 1
  • 19
  • 25