rfc:internal_constructor_behaviour
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:internal_constructor_behaviour [2015/03/01 14:39] – created danack | rfc:internal_constructor_behaviour [2015/03/15 15:39] – Updated with list of exceptions + link to code. danack | ||
---|---|---|---|
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 | + | ii) setting the behaviour |
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 | + | |
- | * 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 | + | Having a different behaviour for internal classes is inconsistent and highly |
- | ``` | + | < |
$mf = new MessageFormatter(' | $mf = new MessageFormatter(' | ||
if ($mf === null) { | if ($mf === null) { | ||
echo " | echo " | ||
} | } | ||
- | ``` | + | </ |
- | 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 | + | 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, | + | The classes IntlDateFormatter, |
- | PHP is a multi-paradigm programming language which support | + | PHP is a multi-paradigm programming language which supports |
- | ``` | + | < |
class NumberFormatter { | class NumberFormatter { | ||
static function create($locale , $style , $pattern = null) { | static function create($locale , $style , $pattern = null) { | ||
Line 62: | Line 62: | ||
} | } | ||
} | } | ||
- | ``` | + | </ |
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. | ||
- | ``` | + | < |
<?php | <?php | ||
// | // | ||
Line 79: | Line 79: | ||
var_dump($reflection-> | var_dump($reflection-> | ||
//Fatal error: ReflectionFunctionAbstract:: | //Fatal error: ReflectionFunctionAbstract:: | ||
- | ``` | + | </ |
- | 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 | + | 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 |
The list of classes that show this behaviour is: | The list of classes that show this behaviour is: | ||
- | * 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. | ||
- | ``` | + | < |
$foo = new PDORow(); | $foo = new PDORow(); | ||
// PHP Fatal error: | // PHP 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. | 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: | ||
- | ``` | + | < |
$mf = new MessageFormatter(' | $mf = new MessageFormatter(' | ||
if ($mf === null) { | if ($mf === null) { | ||
// error handling code | // error handling code | ||
} | } | ||
- | ``` | + | </ |
could be changed to using the factory method: | could be changed to using the factory method: | ||
- | ``` | + | < |
$mf = MessageFormatter:: | $mf = MessageFormatter:: | ||
if ($mf === null) { | if ($mf === null) { | ||
// error handling code | // error handling 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 161: | Line 165: | ||
The vote requires 2/3 to pass. | The vote requires 2/3 to pass. | ||
- | Should the standard paradigm for constructors for internal objects | + | Should the standard paradigm for constructors for internal objects be to 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 clases | + | These classes |
- | * finfo | + | |
- | * PDO | + | * PDO |
- | * Collator | + | * Collator |
- | * IntlDateFormatter | + | * IntlDateFormatter |
- | * MessageFormatter | + | * MessageFormatter |
- | * NumberFormatter | + | * NumberFormatter |
- | * ResourceBundle | + | * ResourceBundle |
- | * IntlRuleBasedBreakIterator | + | * IntlRuleBasedBreakIterator |
- | 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. | + | These classes would be corrected by detecting the invalid data in the constructor and throwing an exception at object construction time, rather |
- | * UConverter | + | * UConverter |
- | * SplFixedArray | + | * SplFixedArray |
- | * ReflectionFunction | + | * ReflectionFunction |
- | * ReflectionParameter | + | * ReflectionParameter |
- | * ReflectionMethod | + | * ReflectionMethod |
- | * ReflectionProperty | + | * ReflectionProperty |
- | * ReflectionExtension | + | * ReflectionExtension |
- | * ReflectionZendExtension | + | * ReflectionZendExtension |
- | * Phar | + | * Phar |
- | * PharData | + | * PharData |
* PharFileInfo | * PharFileInfo | ||
Line 193: | Line 197: | ||
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
- | I hope to fix all of these in a branch before opening the vote, so that people can see the details of the changes. | ||
- | 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. | + | The changes have been made in this branch: https:// |
+ | |||
+ | |||
+ | The list of exceptions | ||
+ | |||
+ | Exception | ||
+ | |||
+ | IntlException -UConverter, | ||
+ | |||
+ | InvalidArgumentException - SplFixedArray | ||
+ | |||
+ | PDOException - PDO, PDORow | ||
+ | |||
+ | PharException - Phar, PharData, PharFileInfo | ||
+ | |||
+ | ReflectionException - ReflectionExtension, | ||
rfc/internal_constructor_behaviour.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1