note 115012 added to language.oop5.final
| From: | cottton at i-stats dot net | Date: | Mon, 12 May 2014 18:03:01 +0000 |
| Subject: | note 115012 added to language.oop5.final | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-199521@lists.php.net to get a copy of this message | ||
imo good to know:
<?php
class BaseClass
{
protected static $var = 'i belong to BaseClass';
public static function test()
{
echo '<hr>'.
'i am
'.__METHOD__.'() and this is my var:
'.self::$var.'<br>';
}
public static function changeVar($val)
{
self::$var = $val;
echo '<hr>'.
'i am '.__METHOD__.'() and i just changed my $var to:
'.self::$var.'<br>';
}
final public static function dontCopyMe($val)
{
self::$var = $val;
echo '<hr>'.
'i am '.__METHOD__.'() and i just changed my $var to:
'.self::$var.'<br>';
}
}
class ChildClass extends BaseClass
{
protected static $var = 'i belong to ChildClass';
public static function test()
{
echo '<hr>'.
'i am '.__METHOD__.'() and this is my var:
'.self::$var.'<br>'.
'and this is my parent var: '.parent::$var.'';
}
public static function changeVar($val)
{
self::$var = $val;
echo '<hr>'.
'i am '.__METHOD__.'() and i just changed my $var to:
'.self::$var.'<br>'.
'but the parent $var is still: '.parent::$var.'';
}
public static function dontCopyMe($val) // Fatal error: Cannot override final method
BaseClass::dontCopyMe() in ...
{
self::$var = $val;
echo '<hr>'.
'i am '.__METHOD__.'() and i just changed my $var to:
'.self::$var.'<br>';
}
}
BaseClass::test(); // i am BaseClass::test() and this is my var: i belong to
BaseClass
ChildClass::test(); // i am ChildClass::test() and this is my var: i belong to
ChildClass
// and this is my parent var: i belong to BaseClass
ChildClass::changeVar('something new'); // i am ChildClass::changeVar() and i
just changed my $var to: something new
// but the parent $var is still: i belong to
BaseClass
BaseClass::changeVar('something different'); // i am BaseClass::changeVar()
and i just changed my $var to: something different
BaseClass::dontCopyMe('a text'); // i am BaseClass::dontCopyMe() and i just
changed my $var to: a text
ChildClass::dontCopyMe('a text'); // Fatal error: Cannot override final method
BaseClass::dontCopyMe() in ...
?>
----
Server IP: 72.52.91.14
Probable Submitter: 146.52.150.253
----
Manual Page -- http://php.net/manual/en/language.oop5.final.php
Edit -- https://master.php.net/note/edit/115012
Del: integrated -- https://master.php.net/note/delete/115012/integrated
Del: useless -- https://master.php.net/note/delete/115012/useless
Del: bad code -- https://master.php.net/note/delete/115012/bad+code
Del: spam -- https://master.php.net/note/delete/115012/spam
Del: non-english -- https://master.php.net/note/delete/115012/non-english
Del: in docs -- https://master.php.net/note/delete/115012/in+docs
Del: other reasons-- https://master.php.net/note/delete/115012
Reject -- https://master.php.net/note/reject/115012
Search -- https://master.php.net/manage/user-notes.php