Your mistake is using a non-static property, while calling a static function. You can see the php notice about it:
PHP Notice: Undefined variable: stringerr in
file.php on line 19
Method 1
You should either define the property as static and apply to it via class name:
<?php
class Error
{
const lexer = 1000 ;
const parser = 2000 ;
const scanner = 3000 ;
}
final class err extends Error
{
public static $stringerr = array (
Error::lexer => "Lexer error" ,
Error::parser => "Parser error" ,
Error::scanner => "Scanner error"
) ;
public static function error ( $x )
{
echo "\nx" . " ? " . err::$stringerr[ $x ] . " error ";
return $x ;
}
}
echo err::error( err::scanner ) ;
?>
Prints:
x ? Scanner error error 3000
Mention, that I deliberately defined error function as static.
Method 2
Or define an instance of this class and use $this
:
<?php
class Error
{
const lexer = 1000 ;
const parser = 2000 ;
const scanner = 3000 ;
}
final class err extends Error
{
public $stringerr = array (
Error::lexer => "Lexer error" ,
Error::parser => "Parser error" ,
Error::scanner => "Scanner error"
) ;
public function error ( $x )
{
echo "\nx" . " ? " . $this->stringerr[ $x ] . " error ";
return $x ;
}
}
$var = new err();
echo $var->error( Error::scanner ) ;
?>
http://www.php.net/manual/en/language.oop5.static.php