Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i'm new in php and i have problems with this piece of code

i do not figure out , why 'echo' prints : x ? error 3000

3000 is a key , indeed should be printing "scanner error" ...

what's wrong in my code ? thank you to every one ...

    <?php

// ....................................... sym
class Error
{
 const lexer    =   1000 ;
 const parser   =   2000 ;
 const scanner  =   3000 ;

}

final class err extends Error
{
 public $stringerr = array (
    lexer       =>  "Lexer error" ,
    parser      =>  "Parser error" ,
    scanner     =>  "Scanner error" 
 ) ; 
 public function error ( $x )
 {
    echo "\nx" . " ? " . $stringerr[ $x ] . " error ";


    return $x ;
 }

}


?>


    echo err::error( err::scanner ) ;

output :

   x ? error 3000
share|improve this question
Just put echo $x before this line - echo "\nx" . " .... You will understand what's wrong. – Shukla Jay シ Aug 1 at 16:06
add comment (requires an account with 50 reputation)

3 Answers

up vote 2 down vote accepted

If I've understood correctly

err::scanner = 3000

$stringerr[ $x ] == $stringerr[3000]

You would need to do err::error('scanner');

or:

public $stringerr = array (
    1000 => "Lexer error",
    2000 => "Parser error",
    3000 => "Scanner error" 
);

And, $stringerr[ $x ] should be $this->stringerr[$x]

share|improve this answer
add comment (requires an account with 50 reputation)

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

share|improve this answer
Thank you , works perfectly ! – Claudio Daffra 2 days ago
add comment (requires an account with 50 reputation)

echo is for printing on the screen + in the function you are returning $x that didn't receive any changes.

Try:

return $stringerr[$x];

Instead of:

echo "\nx" . " ? " . $stringerr[ $x ] . " error ";
return $x;
share|improve this answer
this won't work – user4035 Aug 1 at 16:13
I believe it's a good hint to start with, I can edit it after I know what is it displaying. – heytools Aug 1 at 16:17
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.