定义:
<?php
class foo-----定义类
{
function do_foo()---类的方法
{
echo "Doing foo.";
}
}
$bar = new foo;----实例化类
$bar->do_foo();---调用类的方法
?>
当函数是有条件被定义时,必须在调用函数之前定义。
foo()------------出错,函数未定义
if ($condition) {
function foo()
{
echo "I don't exist until program execution reaches me.\n";
}
}
foo()------------如果$condition为true则可以调用,如果是false则不可以调用?(我猜的)
-------------------------------------------------------------------------------------------------------------------
function foo()
{
function bar()
{
echo "I don't exist until foo() is called.\n";
}
}
/* 现在还不能调用bar()函数,因为它还不存在 */
foo();
/* 现在可以调用bar()函数了,因为foo()函数
的执行使得bar()函数变为已定义的函数 */
bar();
----------------------------------------
返回函数引用
<?php
function &returns_reference()
{
return $someref;
}
$newref =& returns_reference();
?>
可变函数
function foo() {
echo "In foo()<br />\n";
}
function bar($arg = '') {
echo "In bar(); argument was '$arg'.<br />\n";
}
// 使用 echo 的包装函数
function echoit($string)
{
echo $string;
}
$func = 'foo';
$func(); // This calls foo()
$func = 'bar';
$func('test'); // This calls bar()
$func = 'echoit';
$func('test'); // This calls echoit()
调用静态方法
<?php
class Foo
{
static $variable = 'static property';
static function Variable()
{
echo 'Method Variable called';
}
}
echo Foo::$variable; // This prints 'static property'. It does need a $variable in this scope.
$variable = "Variable";//这个应该可以改变静态variable的值(我猜)
Foo::$variable(); // This calls $foo->Variable() reading $variable in this scope.
?>
继承父类变量
$message = 'hello';
// Inherit by-reference
$example = function () use (&$message) {
var_dump($message);
};
echo $example();