0

I am having this class.

<?php

class Helper
{
    private $config;

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

    public function getVal($key)
    {
        return $this->config[$key];
    }
}

The config is set on boot time and cannot be changed during runtime, so for a specific runtime, the same arguments will always give the same results but we cannot say the same thing about different instances of the program.

Can getVal(string) be considered a pure function?

Another, non-OOP version of the same functionality would be this:

<?php

function getVal($key){
    static $config;

    if ($config === null) {
        $config = include "config.php";
    }

    return $config[$key];
}
Loupax
  • 4,728
  • 6
  • 41
  • 68
  • 2
    Since this is OOP and not FP, and PHP doesn't make certain FP guarantees, the definition gets a bit wishy washy. *If you do not modify the object instance*, it behaves pure. The object instance doesn't appear to be mutable… unless you really really want to (*cough* reflection). – deceze Mar 03 '17 at 10:31
  • I guess the OOP example introduced some noise to the question so I added a functional version of it :) – Loupax Mar 03 '17 at 10:41
  • 1
    Now *that* is clearly impure, as it can be manipulated indirectly before its first call. – deceze Mar 03 '17 at 10:46
  • I guessed so the moment I made the edit... – Loupax Mar 03 '17 at 10:49

0 Answers0