PHP OOP හි ස්ථිතික ක්රම සහ $this
ස්ථිතික ක්රම තුළ $this ප්රවේශ විය නොහැක.
මෙය සිදුවන්නේ ස්ථිතික ක්රම
වස්තු ප්රසන්ගයෙන් බැහැරව ඇමතිය හැකි,
පන්තියේ නම වෙත සරලව යොමු වීමෙන්.
සාමාන්ය ක්රමවල ස්ථිතික, එමෙන්ම සාමාන්ය ගුණාංග සහ ක්රම ප්රවේශ විය හැක.
අපි උදාහරණ මගින් බලමු. සාමාන්ය ක්රමයකට උදාහරණය:
<?php
class Test
{
public static $staticProperty = 'static';
public $usualProperty = 'usual';
public function method()
{
echo self::$staticProperty; // 'static' ප්රතිදානය කරයි
echo $this->usualProperty; // 'usual' ප්රතිදානය කරයි
}
}
$test = new Test;
$test->method();
?>
ස්ථිතික ක්රමයකට උදාහරණය:
<?php
class Test
{
public static $staticProperty = 'static';
public $usualProperty = 'usual';
public static function method()
{
echo self::$staticProperty; // 'static' ප්රතිදානය කරයි
echo $this->usualProperty; // දෝෂයක් ලබා දෙයි
}
}
$test = new Test;
$test::method();
?>