0

All I ever see of the PHP Strict Standards are errors reported when (ini_get('error_reporting') & E_STRICT) == true. Correcting these errors in an error driven way seems to be not optimal.

So in order to write code that is perfectly compliant with the PHP Strict Standards out of the box I would like to read what is defined in them. Where can I find the PHP Strict Standards?

All the searching I have done only leads to instructions of how to fix some arbitrary error reported by the Strict Standards, but never the Strict Standards themselves. Can anyone please provide a link?

A. R. Younce
  • 1,913
  • 17
  • 22
Armin
  • 184
  • 1
  • 9
  • 1
    I'm not sure if a definitive list exists, but it should. – DaveRandom Dec 05 '14 at 15:08
  • To clarify: you want to know all possibilities of an `E_STRICT` being emitted? – Levi Morrison Dec 05 '14 at 15:09
  • 1
    To Clarify: yes, absolutely. We are talking about the Strict Standards. I would really like to know what these Standards are, in a coherent way and not by writing the most error prone code ever and evaluating the error log. To further clarify, I don't really want all possibilities of E_Strict being thrown, more the complete and coherent definition of the standards. E_Strict should always be thrown, when those are violated. – Armin Dec 05 '14 at 15:11
  • possible duplicate of [What does E\_STRICT do?](http://stackoverflow.com/questions/14245859/what-does-e-strict-do) – Mark Amery Jan 26 '15 at 23:27

3 Answers3

8

The only way to know where all possibilities for an E_STRICT to be emitted would be to grep the source looking for E_STRICT. Basically, look here at the master branch: http://lxr.php.net/search?q=&defs=&refs=E_STRICT&path=Zend%2F&hist=&project=PHP_TRUNK. Note that in some cases master may differ from a particular version in what E_STRICT errors are raised and when.

Of course, understanding PHP's source would be difficult without an understanding of C and some of the common internals terminology.

Levi Morrison
  • 19,116
  • 7
  • 65
  • 85
  • Nice list. Will have to remember that for myself – Machavity Dec 05 '14 at 15:17
  • 1
    Thank you for the link, but if that is the way to track down the PHP Strict Standards, than we are talking about a very obscene usage of the word Standards. This more or less leaves you with error driven development. If no one can come up with something more human readable I will accept this answer. But I still have hope, even though we are talking about PHP. – Armin Dec 05 '14 at 15:23
  • Seeing you are working on the PHP documentation I don't expect a better source for an anser. It is not the answer I had hoped for, but that doesn't make it any less an answer. Please keep up the work on PHP documentation and maybe start a section on the PHP Standards ;) – Armin Dec 05 '14 at 15:45
  • @Armin The "Strict Standards" doesn't refer to some standards document. The strict standards *are* the error messages. They're just things you shouldn't be doing. There's no manual of style. – Andrea Dec 05 '14 at 16:45
8

Below is a complete list of the possible E_STRICT error messages in PHP 5.6 and bundled extensions (derived from http://lxr.php.net/s?refs=E_STRICT&project=PHP_5_6), along with a brief code sample that will provoke them.

In PHP 5.5, calling any mysql_* function would also produce an E_STRICT, as of PHP 5.6 it produces an E_NOTICE.

There are likely other places that emit them in PECL extensions, feel free to edit them in here if you find one.


Accessing static property ClassName::$propName as non static

class ClassName
{
    public static $propName = 1;
}
$o = new ClassName;
echo $o->propName; // error here

Resource ID#1 used as offset, casting to integer (1)

$fp = fopen('file.txt', 'r');
$array[$fp] = 'something'; // error here
// it's worth noting that an explicit cast to int has the same effect with no error:
$array[(int)$fp] = 'something'; //works

Non-static method ClassName::methodName() should not be called statically (may include additional text: assuming $this from compatible context OtherClassName)

class ClassName
{
    public function methodName()
    {
        return 1;
    }
}
echo ClassName::methodName(); // error here

Only variables should be assigned by reference

function func()
{
    return 1;
}
$var = &func(); // error here

Only variables should be passed by reference

function func(&$arg)
{
    $arg = 1;
}
function func2()
{
    return 0;
}
func(func2()); // error here

Static function ClassName::methodName() should not be abstract

abstract class ClassName
{
    abstract public static function methodName(); // error here
}
class OtherClassName extends ClassName
{
    public static function methodName()
    {
        return 1;
    }
}

Redefining already defined constructor for class ClassName

// Emitted when both a PHP4-style and PHP5-style constructor are declared in a class
class ClassName
{
    public function ClassName($arg)
    {
    }
    public function __construct($arg) // error here
    {
    }
}

Declaration of ClassName::methodName() should be compatible with OtherClassName::methodName()

// Emitted when a class declaration violates the Liskov Substitution Principle
// http://en.wikipedia.org/wiki/Liskov_substitution_principle
class OtherClassName
{
    public function methodName()
    {
        return 1;
    }
}
class ClassName extends OtherClassName
{
    public function methodName($arg) // error here
    {
        return $arg + 1;
    }
}

You should be using the time() function instead

// Emitted when calling mktime() with no arguments
$time = mktime(); // error here

Only basic entities substitution is supported for multi-byte encodings other than UTF-8; functionality is equivalent to htmlspecialchars

// Emitted when using a multi-byte character set that is not UTF-8 with
// htmlentities and some related functions
echo htmlentities("<Stuff>", ENT_COMPAT | ENT_HTML401, '936'); // error here

There is no next result set. Please, call mysqli_stmt_more_results()/mysqli_stmt::more_results() to check whether to call this function/method

// Emitted by mysqli_next_result() when there are no more results
do {
    // stuff
} while (mysqli_next_result($link)); // error here
DaveRandom
  • 87,921
  • 11
  • 154
  • 174
  • Thank you for the list. Do these `E_STRICT` Warnings/Errors somehow uniquely identify themselves? Maybe their error string is the identifier. If there is a way to tell them apart it would be nice to include that identifier and mark it as such (for later reference and tracking of changes). Thanks for your effort so far. Though seeing there being so few it makes it even less understandable to me, way they aren't documented. – Armin Dec 10 '14 at 14:02
  • Nice work - I imagine this took some hours to compile, since I [did the same](http://stackoverflow.com/a/25826279/1709587) on a similar question and it was a hell of a job. It's nice to have something to check my work against. I think you missed one here - the warning for identically declaring a property in both a trait and a class that uses that trait, which is point #8 in my list. – Mark Amery Jan 26 '15 at 23:39
0

There isn't one unified place that lists all of the strict errors but I wouldn't necessarily expect one either. That list would be enormous.

What you can do is look for E_STRICT notices. A common place that will list these is the Migration List that is put out when PHP minor versions (i.e. 5.X) are release. Here's the 5.4 backwards incompatability list, which shows what has E_STRICT notices now. I think this is the best you can get.

Community
  • 1
  • 1
Machavity
  • 30,841
  • 27
  • 92
  • 100
  • That is what I am currently doing and what is driving me nuts. But thank you for the hint. – Armin Dec 05 '14 at 15:19
  • I made a list from lxr, turns out it's not as big as I thought it would be. It's likely to grow a lot in 5.7 (if that actually happens) I suspect. – DaveRandom Dec 05 '14 at 16:15