I am trying to identify pure functions in PHP code.
A pure function is one where both these statements about the function hold:
(definition from Wikipedia)
Is it sufficient to say that a PHP function is pure if and only if
&
in the argument list)$this
in the function body)global
in the function body)$_
variables)Are these statements true ? Am I missing any use cases ?
A pure function is a function that, given the same input, will always return the same output and are side-effect free. // This is a pure function function add($a, $b) { return $a + $b; } Some side-effects are changing the filesystem, interacting with databases, printing to the screen.
In computer programming, a pure function is a function that has the following properties: the function return values are identical for identical arguments (no variation with local static variables, non-local variables, mutable reference arguments or input streams), and.
A function is called pure function if it always returns the same result for same argument values and it has no side effects like modifying an argument (or global variable) or outputting something. The only result of calling a pure function is the return value. Examples of pure functions are strlen(), pow(), sqrt() etc.
rand()
static
variablesglobal
import
/require
statements inside functionsob_get_contents()
There is probably a lot of stuff I'm not thinking of. PHP has a very stateful design.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With