note 114647 deleted from language.oop5.static by cmb
| From: | cmb@php.net | Date: | Sun, 01 Sep 2019 07:18:09 +0000 |
| Subject: | note 114647 deleted from language.oop5.static by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212044@lists.php.net to get a copy of this message | ||
Note Submitter: Denis Ribeiro - dpr001 at gmail dot com
----
Other point is that static methods just can access static properties, because the pseudo variable
$this not exists in this scope like example below:
<?php
//is wrong, because the static methods can only access static properties
class Foo {
public $property;
public static function aStaticMethod() {
echo "Accessing the property: {$this->property}"; //Fatal error:Using $this
}
}
Foo::aStaticMethod();
$classname = 'Foo';
$classname::aStaticMethod(); // As of PHP 5.3.0
//Note that property $property is static and then can be using the word self instead of pseudo
variable $this
class Foo2 {
static $property = 'test';
public static function aStaticMethod() {
echo "Accessing the property: ". self::$property; //Accessing the property: test
}
}
Foo2::aStaticMethod();