Hi Internals

The main gist:
--------------

I suggest to introduce an official type-checker / static analyser /
preprocessor / transpiler for PHP, somewhat similar to TypeScript in
the JavaScript world, which will allow to introduce the features
otherwise impossible for the PHP world, at the cost of including an
additional transpilation step.

The reasoning:
--------------

You all know there are a lot of problems with the language, especially
large gaps with the current type system, which have large community
demand to overcome, but still cannot be solved due to the paradigm
chosen by PHP. And the paradigm is to be statically typed (to some
extent), but dynamically checked language, which limits the number of
available options due to performance reasons.

The introduction of static types have gradually eliminated the need of
using external means of communicating the types (phpdoc @param and
@return annotations), replacing them with native language constructs,
making the language more expressive and enterprise-ready. However,
there are a lot of things still missing, like types of arrays,
including lists, hash-maps and structure-like arrays, and of course
generic classes and functions. These are still covered nowadays with
external means like phpdoc annotations and static analysers / type
checkers to validate them. This leaves PHP in the odd mixture of
native types and externally validated annotations via comments.

Paradigm of a dynamically checked language also leaves out the problem
of type-checking before running the code. Even though the types are
defined statically, you still have to physically run the code, with
all possible execution paths, to catch a type error. To avoid that you
still would have to use an external typechecker / static analyser,
even if PHP could cover 100% of type demand natively.

Python solves this problem nicely I think. It is dynamically typed,
but it allows a special syntax for static types and also has a
separate static type checker. I think this is really a good approach
and it would be great if PHP followed this path for the beginning, but
we have what we have already.

There were attempts to create preprocessors for PHP (like PHP++ IIRC),
which allowed features like generics, but they did not gain enough
popularity, weren't baked by large enough companies, didn't make their
way to major IDEs support. I believe the only solution is to make an
official one.

Therefore, I suggest the following thing:
1. Introduce a new #declare directive which will mark the file as
requiring pre-processing.
2. Let PHP introduce special features that only work in such
"pre-processing required" files, such as typed lists and generics.
3. Implement a static type-checker in PHP that will verify those typed
lists and generics are indeed used in a valid way.
4. Make the PHP preprocessor output the "normal" PHP file somewhere,
which has list types and generics erased. I think something like that
was suggested earlier many times.

As an alternative to a new #declare feature + validation +
transpilation in "plain old PHP", the Python way of just ignoring
statically checked stuff could be applied. This approach though looks
odd to me in PHP world, because having some stuff checked in runtime,
and some only statically may confuse developers a lot.

Basically, this will split PHP in two languages, one being superset of
other, but checked statically. I realize this might be much of a
paradigm shift, it may require lots of work if accepted (I can only
dream of it though), but still I want to share my thoughts with you
and propose something for consideration.

Regards,
Illia / someniatko

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to