Section 13.6. E_STRICT

13.6. E_STRICT

Besides the real backward-compatibility breaks previously discussed, there are also a number of "deprecated" features. Deprecated features emit an E_STRICT error, which is not part of the E_ALL error setting. To see those deprecated issues in PHP 4 code, you need to set error reporting to E_ALL | E_STRICT.


Because PHP 4 does not understand the E_STRICT constant, you might want to use the numerical version to make the scripts run with both PHP 4 and PHP 5. The numerical value for E_STRICT is 2048. To show all errors (E_ALL and E_STRICT), you need to use the value 4095 for either the error_reporting() function or as php.ini setting.

13.6.1. Automagically Creating Objects

In PHP 4, the following code would automagically create an object $person of class StdClass:

 <?php      $person->name = "Derick"; ?> 

PHP 5 still allows this, but throws the E_STRICT error Creating default object from empty value. To prevent this error, use $person = new StdClass; before the property-assignment. This also works with PHP 4.

13.6.2. var and public

Using var to specify a property of an object is now deprecated. Using public is recommended. Using var rather than public throws the E_STRICT error var: Deprecated. Please use the public/private/protected modifiers. If your code also needs to run on PHP 4, you can safely ignore this "error."

13.6.3. Constructors

With PHP 5, a new style of "unified" constructor is introduced: __construct(). If you are migrating existing PHP 4 code that uses __construct() as a method name, you can get unexpected results. If both the PHP 4 style constructor (classname()) and the PHP 5 style constructor (__construct()) are defined, an E_STRICT error is thrown: Redefining already defined constructor for class <classname>, as you can see in the output of the following example:

 <?php      class person {            var $name;            function __construct($name)            {                  echo __FUNCTION__, "\n";                  $this->name = $name;            }            function person($name)            {                  echo __FUNCTION__, "\n";                  $this->name = $name;            }      }      $person = new person('Derick'); ?> 

Only the PHP 5 style constructor is used, no matter which is declared first in the class.

13.6.4. Inherited Methods

Consider the following example:

 <?php      class magazine {            var $title;            function getTitle() {                  return $this->title;            } }      class issues extends magazine {            var $issues;            function getTitle($nr) {                  return ($this->title. ' - '. $this->issues[$nr]);            }      }         $mag = new issues;         $mag->title = "Time";         $mag->issues = array (1 => 'Jan 2003', 2 => 'Feb 2003');         echo $mag->getTitle(2);         ?> 

The signature of the getTitle() method is different in the inherited class. It accepts an additional parameter ($nr). Because this violates the OO contracts, PHP 5 throws an E_STRICT error: Declaration of issues::getTitle() must be compatible with that of magazine::getTitle(). Adding a dummy argument to the magazine::getTitle() method, such as function getTitle($dummy), is a simple workaround.

13.6.5. Define Classes Before Usage

It's a good idea to declare your classes in your code before you start using themfor example, in an include file. Although it's not always necessary, you need to declare the class before using it when you work with the more advanced OO features of PHP 5, such as interfaces.

    PHP 5 Power Programming
    PHP 5 Power Programming
    ISBN: 013147149X
    EAN: 2147483647
    Year: 2003
    Pages: 240

    Similar book on Amazon © 2008-2017.
    If you may any questions please contact us: