Skip to content

Instantly share code, notes, and snippets.

@FLamparski
Created September 16, 2015 08:48
Show Gist options
  • Save FLamparski/92c876599c2d75acc94f to your computer and use it in GitHub Desktop.
Save FLamparski/92c876599c2d75acc94f to your computer and use it in GitHub Desktop.
In PHP, functions "are" "first" "class" "objects"
// Psy Shell v0.4.4 (PHP 5.6.4-4ubuntu6.2 — cli) by Justin Hileman
>>> function foo($x) { return $x * 2; }
=> null
>>> foo(5)
=> 10
>>> is_callable(foo)
PHP error: Use of undefined constant foo - assumed 'foo' on line 1
>>> is_callable('foo')
=> true
>>> 'foo'(5)
PHP Parse error: Syntax error, unexpected '(' on line 1
>>> ('foo')(5)
PHP Parse error: Syntax error, unexpected '(' on line 1
>>> $bar = 'foo';
=> "foo"
>>> is_callable($bar)
=> true
>>> $bar(5)
=> 10
>>> is_callable('array_map')
=> true
>>> $map = 'array_map';
=> "array_map"
>>> $map($bar, [1, 2, 3])
=> [
2,
4,
6
]
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment