Exemple #1
0
<?php

## Вывод дерева вызовов функции.
function inner($a)
{
    // Внутренняя функция.
    echo "<pre>";
    print_r(debug_backtrace());
    echo "</pre>";
}
function outer($x)
{
    // Родительская функция.
    inner($x * $x);
}
// Главная программа.
outer(3);
<?php

class Orator
{
    private $name;
    function __construct($name)
    {
        $this->name = $name;
        echo "Создан объект {$this->name}.<br>";
    }
    function __destruct()
    {
        echo "Уничтожен объект {$this->name}.<br>";
    }
}
function outer()
{
    $obj = new Orator(__METHOD__);
    echo "Внимание, вбрасывание!<br>";
    throw new Exception("Hello!");
}
echo "Начало прогарми.<br>";
try {
    echo "Начало try-блока.<br>";
    outer();
    echo "Конец try-блока.<br>";
} catch (Exception $e) {
    echo " Исключение: {$e->getMessage()}<br>";
}
echo "Конец програмы.<br>";
<?php

function outer($msg)
{
    function inner($msg)
    {
        echo 'inner: ' . $msg . ' ';
    }
    echo 'outer: ' . $msg . ' ';
    inner($msg);
}
inner('test1');
// Fatal error:  Call to undefined function inner()
//上面出错,是因为外部函数还没有调用,所以出错。
outer('test2');
// outer: test2 inner: test2
inner('test3');
// inner: test3
outer('test4');
// Fatal error:  Cannot redeclare inner()
//上面出错,是因为,外部函数被调用时,内部函数被重定义了