note 68304 deleted from language.variables.scope by cmb
| From: | cmb@php.net | Date: | Sun, 27 Oct 2019 08:32:18 +0000 |
| Subject: | note 68304 deleted from language.variables.scope by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212784@lists.php.net to get a copy of this message | ||
Note Submitter: sami doesn't want spam at no-eff-eks com
----
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!