Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asm89/6886578 to your computer and use it in GitHub Desktop.
Save asm89/6886578 to your computer and use it in GitHub Desktop.
TIL: Nested functions in PHP
<?php
class NestedFunctions
{
public function fn1()
{
function nested_fn() { echo 'yay' . "\n"; }
nested_fn();
}
public function fn2()
{
nested_fn();
}
}
$nested = new NestedFunctions();
$nested->fn2();
$nested->fn1();
// output: http://3v4l.org/hrNsX
<?php
class NestedFunctions
{
public function fn1()
{
function nested_fn() { echo 'yay' . "\n"; }
nested_fn();
}
}
$nested = new NestedFunctions();
$nested->fn1();
$nested->fn1();
// output: http://3v4l.org/buK93
<?php
class NestedFunctions
{
public function fn1()
{
function nested_fn() { echo 'yay' . "\n"; }
nested_fn();
}
public function fn2()
{
function nested_fn() { echo 'yay' . "\n"; }
nested_fn();
}
}
$nested = new NestedFunctions();
$nested->fn1();
$nested->fn2();
// output: http://3v4l.org/WABfQ
<?php
class NestedFunctions
{
public function fn1()
{
function nested_fn() { echo 'yay' . "\n"; }
nested_fn();
}
public function fn2()
{
nested_fn();
}
}
$nested = new NestedFunctions();
$nested->fn1();
$nested->fn2();
// output: http://3v4l.org/2V57M
<?php
class NestedFunctions
{
public function fn1()
{
function nested_fn() { echo 'yay' . "\n"; }
nested_fn();
}
}
$nested = new NestedFunctions();
$nested->fn1();
// output: http://3v4l.org/5brpq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment