示例#1
0
<?php

class Foo
{
}
function bar(Foo $o)
{
    var_dump($o);
}
function bar2(Foo $o = null)
{
    var_dump($o);
}
// this will generate an error, which is good
bar(null);
// this is ok
bar2(null);
示例#2
0
// global scope
echo '<p>' . $c . '</p>';
echo '<p>' . foo(10, 5) . '</p>';
echo '<p>' . $c . '</p>';
// $c is unchanged
// pass by value example
function bar($a)
{
    $a = $a * $a;
    return $a;
}
$c = 10;
// global scope
echo '<p>' . $c . '</p>';
echo '<p>' . bar($c) . '</p>';
// value of $c is copied to $a
echo '<p>' . $c . '</p>';
// $c is unchanged
// pass by reference example
function bar2(&$a)
{
    // & means reference to ORIGINAL var
    $a = $a * $a;
    return $a;
}
$c = 10;
// global scope
echo '<p>' . $c . '</p>';
echo '<p>' . bar2($c) . '</p>';
echo '<p>' . $c . '</p>';
// $c is changed