Metodat Statike dhe $this në OOP në PHP
Brenda metodave statike $this nuk është i disponueshëm.
Kjo ndodh sepse metodat statike
mund të thirren jashtë kontekstit të objektit,
thjesht duke iu referuar emrit të klasës.
Në metodat e zakonshme janë të disponueshme si ato statike, ashtu edhe vetitë dhe metodat e zakonshme.
Le të shohim me shembuj. Shembull për një metodë të zakonshme:
<?php
class Test
{
public static $staticProperty = 'static';
public $usualProperty = 'usual';
public function method()
{
echo self::$staticProperty; // do të shfaqë 'static'
echo $this->usualProperty; // do të shfaqë 'usual'
}
}
$test = new Test;
$test->method();
?>
Shembull për një metodë statike:
<?php
class Test
{
public static $staticProperty = 'static';
public $usualProperty = 'usual';
public static function method()
{
echo self::$staticProperty; // do të shfaqë 'static'
echo $this->usualProperty; // do të japë një gabim
}
}
$test = new Test;
$test::method();
?>