RE: [PHP-DEV] Design by Contract
| From: | François Laupretre | Date: | Thu, 05 Feb 2015 17:40:17 +0000 |
| Subject: | RE: [PHP-DEV] Design by Contract | ||
| References: | 1 2 3 4 5 6 7 8 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-81950@lists.php.net to get a copy of this message | ||
De : Dmitry Stogov [mailto:dmitry@zend.com]
>
> I don't like phpdoc approach because we have to define, parse and compile new syntax for
> constraints.
> Would constraint be able to call other functions? include external php files? etc?
There are 2 sort of constraints :
- declared types with a specific, while very intuitive, syntax (support for every current type found
in phpdoc will be provided). These types will be sent as plain strings along with the corresponding
zval argument to an internal validator function. This is a valuable addition compared to the DbC
basic conditions. I gave the example of str_replace() whose search argument would have a type like
'string|array(string)'. Do you think that, if we implement such a check in PHP in an in{}
block, it will be as readable ? I don't think so. OK, it is an addition to what other languages
implement but, if we implement something easier and more readable, the concept is still valid.
- and assertions. An assertion line just contains a PHP expression to evaluate. No new syntax here,
expression can do
anything, like calling an external validator function. The condition would be executed in the
function scope, so it would have access to anything accessible from the function body. Exactly the
same as what we would do with in/out{} blocks.
In my opinion, as I previously explained, constraints shouldn't be parsed by the PHP engine,
but by a pre-processor which would run before any other action takes place. This pre-processor would
insert some PHP code where it is needed. Then, the script would be compiled but the engine
doesn't have to be concerned.
Cheers
François