note 68304 modified in language.variables.scope by danbrown
| From: | danbrown@php.net | Date: | Mon, 18 May 2009 01:35:57 +0000 |
| Subject: | note 68304 modified in language.variables.scope by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155558@lists.php.net to get a copy of this message | ||
PHP 5.1.4 doesn't seem to care about the static keyword. It doesn't let you use $this in a
static method, but you can call class methods through an instance of the class using regular ->
notation. You can also call instance methods as class methods through the class itself. The
documentiation here is plain wrong.
<?php
class Foo {
public static function static_fun()
{
return "This is a class method!\n";
}
public function not_static_fun()
{
return "This is an instance method!\n";
}
}
echo '<pre>';
echo "From Foo:\n";
echo Foo::static_fun();
echo Foo::not_static_fun();
echo "\n";
echo "From \$foo = new Foo():\n";
$foo = new Foo();
echo $foo->static_fun();
echo $foo->not_static_fun();
echo '</pre>';
?>
You'll see the following output:
From Foo:
This is a class method!
This is an instance method!
From $foo = new Foo():
This is a class method!
This is an instance method!
--was--
PHP 5.1.4 doesn't seem to care about the static keyword. It doesn't let you use $this in a
static method, but you can call class methods through an instance of the class using regular ->
notation. You can also call instance methods as class methods through the class itself. The
documentiation here is plain wrong.
class Foo {
public static function static_fun()
{
return "This is a class method!\n";
}
public function not_static_fun()
{
return "This is an instance method!\n";
}
}
echo '<pre>';
echo "From Foo:\n";
echo Foo::static_fun();
echo Foo::not_static_fun();
echo "\n";
echo "From \$foo = new Foo():\n";
$foo = new Foo();
echo $foo->static_fun();
echo $foo->not_static_fun();
echo '</pre>';
You'll see the following output:
From Foo:
This is a class method!
This is an instance method!
From $foo = new Foo():
This is a class method!
This is an instance method!
http://php.net/manual/en/language.variables.scope.php