<?php
Translate::lookup();
class Translate
{
const ENGLISH = 0;
const SPANISH = 1;
const FRENCH = 2;
const GERMAN = 3;
// ...
function lookup()
{
echo self::SPANISH;
}
}
?>
Now I'm supposed to get s simple "1" as output. Instead I get some
warning about "Strict standards" (screenprint: http://i53.tinypic.com/28jkswl.jpg).
Is it some new PHP thingie or is is something I can turn off in my
PHP.ini or what?
I looked through the php.net documentation, but I can't find anything
that easily matches what I'm seeing. So I'd also appreciate if y'all
could tell me how to figure out these kind of errors.
Thanks much.
That message is OK. You are calling a non-static method in a static context.
You will fix that simply, just replace
function lookup()
with
static function lookup()
If you want to manage how error messages are shown.
http://www.php.net/manual/en/errorfunc.configuration.php#ini.display-errors
And
http://www.php.net/manual/en/errorfunc.configuration.php#ini.error-reporting
>I'm working through the O'Reilly book "PHP, MySQLm and JavaScript" by
>Robin Nixon and the following example is in the book.
>
><?php
>Translate::lookup();
>
>class Translate
>{
> const ENGLISH = 0;
> const SPANISH = 1;
> const FRENCH = 2;
> const GERMAN = 3;
> // ...
>
> function lookup()
> {
> echo self::SPANISH;
> }
>}
>?>
>
>
>Now I'm supposed to get s simple "1" as output. Instead I get some
>warning about "Strict standards" (screenprint: http://i53.tinypic.com/28jkswl.jpg).
>
>Is it some new PHP thingie or is is something I can turn off in my
>PHP.ini or what?
It's a sloppiness in your code. The warning pretty much explains it:
You're calling the lookup() method statically, even though it's not
declared as static. There are two possible solutions:
1) Create an instance first, then call the lookup() method:
$foo = new Translate();
$foo->lookup();
2) Declare the method as static:
class Translate {
...
public static function lookup() {
echo self::SPANISH;
}
}
>I looked through the php.net documentation, but I can't find anything
>that easily matches what I'm seeing.
http://www.php.net/manual/en/language.oop5.static.php
| Calling non-static methods statically generates an E_STRICT level
| warning.
Micha
You're trying to call a non-static function without having an object of
the class. Check the errata for the book - I'm seeing mixed reviews on
O'Reilly's web site because of the number of errors in the book.
BTW, rather than put up screen shots, please just the results here -
especially if it's a short message like this. It is much easier to
follow the thread when everything is in one place.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstu...@attglobal.net
==================
Yes, I did this at http://oreilly.com/catalog/errata.csp?isbn=9780596157142,
but that particular error does not seem to be flagged. I'm suspecting
that since it does work, it has to do with the E_STRICT error option
that seems to have been introduced in PHP5. Perhaps the author did not
have it turned on.
Now all that is left for me is to figure out the advice I've been
given here about correcting the code - I can't say that I can wrap my
head around the static issues very well right now.
>
> BTW, rather than put up screen shots, please just the results here -
> especially if it's a short message like this. It is much easier to
> follow the thread when everything is in one place.
>
Yes, sorry about that. It was so colorful, I thought some information
might have been in the way it was displayed, since I'm not used to
programming languages being so elaborate when displaying error
messages.
Thanks much, this worked. And I'll submit this as an errata to the
book.
On a development machine error reporting should be set as high as
possible. E_ALL is a must, E_ALL|E_STRICT would be preferred. _All_
messages, even E_NOTICE errors, should be taken care of.
Micha
IMHO, passing (E_ALL | E_STRICT) is mandatory when you're writing a
book on PHP!