rfc:internal_constructor_behaviour

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
rfc:internal_constructor_behaviour [2015/03/01 14:39] – created danackrfc:internal_constructor_behaviour [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 3: Line 3:
   * Date: 2015-03-01    * Date: 2015-03-01 
   * Author: Dan Ackroyd, Danack@php.net   * Author: Dan Ackroyd, Danack@php.net
-  * Status: Under Discussion+  * Status: Implemented (in PHP 7.0)
   * First Published at: https://wiki.php.net/rfc/internal_constructor_behaviour   * First Published at: https://wiki.php.net/rfc/internal_constructor_behaviour
  
Line 12: Line 12:
 i) to make the behaviour of these classes behave more consistently with the behaviour that most people would expect them to have. i) to make the behaviour of these classes behave more consistently with the behaviour that most people would expect them to have.
  
-ii) setting the coding standards that should be followed by internal classes.+ii) setting the behaviour that should be followed by internal classes for their constructors.
  
 If other internal classes that are not listed in this RFC are found to have behaviour that is not consistent with the behaviour listed in this RFC they should be treated as bugs and fixed at the next appropriate release. If other internal classes that are not listed in this RFC are found to have behaviour that is not consistent with the behaviour listed in this RFC they should be treated as bugs and fixed at the next appropriate release.
Line 21: Line 21:
 For the internal classes: For the internal classes:
  
-* finfo +  * finfo 
-* PDO +  * PDO 
-* Collator +  * Collator 
-* IntlDateFormatter +  * IntlDateFormatter 
-* MessageFormatter +  * MessageFormatter 
-* NumberFormatter +  * NumberFormatter 
-* ResourceBundle +  * ResourceBundle 
-* IntlRuleBasedBreakIterator+  * IntlRuleBasedBreakIterator
  
  
 when their constructor is called with parameters that cannot be used to create a valid instance of the class, the constructor returns null. This is inconsistent with classes which are declared in userland which are not capable of returning null value, but instead have to indicate an error by throwing an exception. when their constructor is called with parameters that cannot be used to create a valid instance of the class, the constructor returns null. This is inconsistent with classes which are declared in userland which are not capable of returning null value, but instead have to indicate an error by throwing an exception.
  
-Having a different behaviour for internal classes is inconsistent and highly suprising when users encounter it:+Having a different behaviour for internal classes is inconsistent and highly surprising when users encounter it:
  
-```+<code>
 $mf = new MessageFormatter('en_US', '{this was made intentionally incorrect}'); $mf = new MessageFormatter('en_US', '{this was made intentionally incorrect}');
 if ($mf === null) { if ($mf === null) {
     echo "Surprise!";     echo "Surprise!";
 } }
-```+</code>
  
-This RFC proposes setting a standard that internal classes should either return a valid instance of the class or throw an exception. Also for the classes listed for them to be changed in PHP 7 so that they follow this behaviour.+This RFC proposes setting a standard that internal classes should either return a valid instance of the class or throw an exception. Also for the classes listed to be changed in PHP 7 so that they follow this behaviour.
  
 === Factory methods === === Factory methods ===
  
-The classes IntlDateFormatter, MessageFormatter, NumberFormatter, ResourceBundle and IntlRuleBasedBreakIterator all have a static factory method that can create an instance of the class, as well as the constructor. The factory methods do not currently throw an exception when invalid arguments are passed to it. It is the position of this RFC that it is correct for those factory methods return either null or a valid usuable instance of the class.+The classes IntlDateFormatter, MessageFormatter, NumberFormatter, ResourceBundle and IntlRuleBasedBreakIterator all have a static factory method that can create an instance of the class, as well as the constructor. The factory methods do not currently throw an exception when invalid arguments are passed to it. It is the position of this RFC that it is correct for those factory methods to return either null or a valid usable instance of the class.
  
-PHP is a multi-paradigm programming language which support writing code in either procedural or OO style. Having the factory methods behave like this is perfectly consistent with userland code. +PHP is a multi-paradigm programming language which supports writing code in either procedural or OO style. Having the factory methods behave like this is perfectly consistent with userland code. 
  
-```+<code>
 class NumberFormatter { class NumberFormatter {
     static function create($locale , $style , $pattern = null) {     static function create($locale , $style , $pattern = null) {
Line 62: Line 62:
     }     }
 } }
-```+</code>
  
 There is also a function `finfo_open` which is also a procedural way of instantiating an finfo object. This function would not be modified. There is also a function `finfo_open` which is also a procedural way of instantiating an finfo object. This function would not be modified.
Line 71: Line 71:
 The constructors of several classes check the parameters that they are given and just give a warning when they are not acceptable. This leaves the object instantiated but in an unusable state. The constructors of several classes check the parameters that they are given and just give a warning when they are not acceptable. This leaves the object instantiated but in an unusable state.
  
-```+<code>
 <?php <?php
 //Reflection function is not meant to accept an array //Reflection function is not meant to accept an array
Line 79: Line 79:
 var_dump($reflection->getParameters()); var_dump($reflection->getParameters());
 //Fatal error: ReflectionFunctionAbstract::getParameters(): Internal error: Failed to retrieve the reflection object in.. //Fatal error: ReflectionFunctionAbstract::getParameters(): Internal error: Failed to retrieve the reflection object in..
-```+</code>
  
-This RFC proposes that this behaviour should be changed so that the constructor should throw an exception if the class cannot be created in usuable state i.e. convert the current warning to an exception of the appropriate.+This RFC proposes that this behaviour should be changed so that the constructor should throw an exception if the class cannot be created in a usable state i.e. convert the current warning to an appropriate exception.
  
 The list of classes that show this behaviour is: The list of classes that show this behaviour is:
  
-* UConverter +  * UConverter 
-* SplFixedArray +  * SplFixedArray 
-* ReflectionFunction +  * ReflectionFunction 
-* ReflectionParameter +  * ReflectionParameter 
-* ReflectionMethod +  * ReflectionMethod 
-* ReflectionProperty +  * ReflectionProperty 
-* ReflectionExtension +  * ReflectionExtension 
-* ReflectionZendExtension +  * ReflectionZendExtension 
-* Phar +  * Phar 
-* PharData +  * PharData 
-* PharFileInfo+  * PharFileInfo
  
  
Line 102: Line 102:
 The class PDORow gives a fatal error when a user attempts to instantiate it. The class PDORow gives a fatal error when a user attempts to instantiate it.
  
-```+<code>
 $foo = new PDORow(); $foo = new PDORow();
 // PHP Fatal error:  PDORow::__construct(): You should not create a PDOStatement manually in test.php on line 5 // PHP Fatal error:  PDORow::__construct(): You should not create a PDOStatement manually in test.php on line 5
-```+</code>
  
 Fatal errors should only be used for fatal errors. This RFC proposes that the constructor for PDORow should be changed to throw an appropriate exception rather than giving a fatal error. Fatal errors should only be used for fatal errors. This RFC proposes that the constructor for PDORow should be changed to throw an appropriate exception rather than giving a fatal error.
 +
 +
 +
 +
  
  
Line 118: Line 122:
 For the classes that have a factory creation method the code that currently tests against the constructor returning null: For the classes that have a factory creation method the code that currently tests against the constructor returning null:
  
-```+<code>
 $mf = new MessageFormatter('en_US', $thisMightBeInvalid); $mf = new MessageFormatter('en_US', $thisMightBeInvalid);
 if ($mf === null) { if ($mf === null) {
     // error handling code     // error handling code
 } }
-```+</code>
  
 could be changed to using the factory method: could be changed to using the factory method:
  
  
-```+<code>
 $mf = MessageFormatter::create('en_US', $thisMightBeInvalid); $mf = MessageFormatter::create('en_US', $thisMightBeInvalid);
 if ($mf === null) { if ($mf === null) {
     // error handling code     // error handling code
 } }
-```+</code>
  
 For the other classes which do not have an equivalent procedural method which will still return null, the user would need to wrap the call to the constructor in a try/catch block. For the other classes which do not have an equivalent procedural method which will still return null, the user would need to wrap the call to the constructor in a try/catch block.
Line 151: Line 155:
 ==== To Existing Extensions ==== ==== To Existing Extensions ====
  
-The standard of either returning a usuable instance or throwing an exception in an objects constructor would apply to all extensions that ship with the official PHP release. Other extensions (e.g. those that are hosted on PECL) may wish to follow this standard, but it is not required.+The standard of either returning a usable instance or throwing an exception in an objects constructor would apply to all extensions that ship with the official PHP release. Other extensions (e.g. those that are hosted on PECL) may wish to follow this standard, but it is not required.
  
 ===== Open Issues ===== ===== Open Issues =====
-If anyone feels strongly about splitting the vote into separate parts, please say so. 
  
 +Some of the Intl extension code has always had the behaviour of giving an error notice, and also throwing an exception for the same error. This behaviour should be cleaned up for the release of PHP 7, so that either an error is given, or an exception, but never both.
  
-===== Proposed Voting Choices ===== 
  
-The vote requires 2/3 to pass.+===== Patches and Tests =====
  
-Should the standard paradigm for constructors for internal objects to be to either return a usable instance of a class on success, and throw an exception if they encounter an error, and should the code for the classes listed below be modified to follow this standard? +These classes will be corrected by making the constructor throw an exception rather than return null if the construction of the object fails.
  
-These clases will be corrected by making the constructor throw an exception rather than return null if the construction of the object fails.+  * finfo 
 +  * PDO 
 +  * Collator 
 +  * IntlDateFormatter 
 +  * MessageFormatter 
 +  * NumberFormatter 
 +  * ResourceBundle 
 +  * IntlRuleBasedBreakIterator
  
-* finfo 
-* PDO 
-* Collator 
-* IntlDateFormatter 
-* MessageFormatter 
-* NumberFormatter 
-* ResourceBundle 
-* IntlRuleBasedBreakIterator 
  
- +These classes would be corrected by detecting the invalid data in the constructor and throwing an exception at object construction time, rather than giving an error when the created instance is used. 
-These classes would be corrected by detecting the invalid data in the constructor and throwing an exception at object construction time, rather giving an error when the created instance is used. +  * UConverter 
-* UConverter +  * SplFixedArray 
-* SplFixedArray +  * ReflectionFunction 
-* ReflectionFunction +  * ReflectionParameter 
-* ReflectionParameter +  * ReflectionMethod 
-* ReflectionMethod +  * ReflectionProperty 
-* ReflectionProperty +  * ReflectionExtension 
-* ReflectionExtension +  * ReflectionZendExtension 
-* ReflectionZendExtension +  * Phar 
-* Phar +  * PharData 
-* PharData +  * PharFileInfo
-* PharFileInfo+
  
  
 The class PDORow will be changed to give an exception if an attempt is made to instantiate it from userland. The class PDORow will be changed to give an exception if an attempt is made to instantiate it from userland.
  
 +The changes have been made in this branch: https://github.com/danack/php-src/tree/InternalClassClean or to view as a PR https://github.com/php/php-src/pull/1178
  
-===== Patches and Tests ===== +The list of exceptions used are: 
-I hope to fix all of these in branch before opening the voteso that people can see the details of the changes.+ 
 +Exception - finfo 
 + 
 +IntlException -UConverter, Collator, IntlDateFormatter, MessageFormatter, NumberFormatter3v, ResourceBundle, IntlRuleBasedBreakIterator 
 + 
 +InvalidArgumentException - SplFixedArray 
 + 
 +PDOException - PDO, PDORow 
 + 
 +PharException - Phar, PharData, PharFileInfo 
 +     
 +ReflectionException - ReflectionExtension,  ReflectionFunction, ReflectionMethod, ReflectionParameter, ReflectionProperty, ReflectionZendExtension 
 + 
 + 
 + 
 +===== Voting ===== 
 + 
 + 
 +Should the standard paradigm for constructors for internal objects be to return a usable instance of a class on successand throw an exception if they encounter an error, and should the code for the classes listed below be modified to follow this standard?  
 + 
 +<doodle title="Constructor behaviour of internal classes" auth="danack" voteType="single" closed="true"> 
 +   * Yes 
 +   * No 
 +</doodle>
  
-At the very least all of the exception to be used for each class will be determined, and will be an appropriate exception i.e. not \Exception.+Voting will close March 29th 2015 9pm UTC and requires 2/3 in favour to pass.
  
  
 ===== Implementation ===== ===== Implementation =====
 After the project is implemented, this section should contain  After the project is implemented, this section should contain 
-  - the version(s) it was merged to +  - the version(s) it was merged to: 7.0  
-  - a link to the git commit(s) +  - a link to the git commit(s): http://git.php.net/?p=php-src.git;a=commit;h=4796e0242b8cdd2a77b552fcbaa74d70d87f6d0a 
-  - a link to the PHP manual entry for the feature+  - a link to the PHP manual entry for the feature: No new manual entry, the changes are conforming to standard practice.
  
  
rfc/internal_constructor_behaviour.1425220798.txt.gz · Last modified: 2017/09/22 13:28 (external edit)