{"id":284,"date":"2024-05-01T13:16:20","date_gmt":"2024-05-01T13:16:20","guid":{"rendered":"https:\/\/miklcct.com\/wordpress\/?p=284"},"modified":"2024-05-01T13:16:22","modified_gmt":"2024-05-01T13:16:22","slug":"the-type-system-of-php-is-no-longer-fit-for-modern-web-programming","status":"publish","type":"post","link":"https:\/\/miklcct.com\/wordpress\/2024\/05\/01\/the-type-system-of-php-is-no-longer-fit-for-modern-web-programming\/","title":{"rendered":"The type system of PHP is no longer fit for modern web programming"},"content":{"rendered":"\n<p>PHP, like Javascript, started as a scripting language for the web, with PHP for server side and Javascript for client side. They were initially designed to add simple, dynamic elements into static web pages in an era when processing power was just a fraction of today, so they were designed to be easy-to-use by people with minimal programming knowledge, for example, they allowed the use of undeclared variables without specifying types, and the operators would try to intepret the operands in their expected type even if a different type was given. For example, in PHP, <code>'3'+'4'<\/code> results in <code>7<\/code>, which makes it convenient to deal with HTML form inputs, which are always strings.<\/p>\n\n\n\n<p>Such &#8220;features&#8221; designed for ease of use in simple functionalities are clearly not fit for purpose when full-featured applications with user interactions (Web 2.0), not just content-delivering websites (Web 1.0), are built and run wholly on the web. Therefore, over time, both PHP and Javascript gain features for &#8220;proper&#8221; programming. For example, in PHP, closures, namespaces, constant expressions, null coalescing operator, type declarations, union types, intersection types are gradually added over time, with the severity of bad programming practices gradually upgraded from <code>Notice<\/code>s to <code>Warning<\/code>s to <code>Error<\/code>s; while in Javascript, strict mode, modules, <code>async<\/code> \/ <code>await<\/code>, null coalescing operator, and more built-in higher-order functions are added as well.<\/p>\n\n\n\n<p>Despite that they are copying features from each other, and from other programming languages, they have used a completely different approach to enhance their type safety.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Type safety in PHP<\/h2>\n\n\n\n<p>In PHP, all values are of one of the following types: the primitive types <code>null<\/code>, <code>int<\/code>, <code>bool<\/code>, <code>float<\/code>, <code>string<\/code>; or the composite types <code>array<\/code>, <code>object<\/code>; or the special <code>resource<\/code> type which is specific to a certain library and can&#8217;t be defined natively. Values of user-defined classes are <code>object<\/code>s, with <code>stdClass<\/code> being the class of <code>object<\/code>s created without specifying a specific class, such as a type cast to an <code>object<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Type declarations<\/h3>\n\n\n\n<p>PHP, like Javascript, did not historically have type declarations. Starting from PHP 5, with the improved class system, it started to become possible to specify types in the parameter list. However, at that time, only classes could be specified. It was not possible to specify that a function parameter should be an <code>int<\/code>.<\/p>\n\n\n\n<p>PHPDoc, a library used to generate documentations for the language, can be used to add &#8220;type hints&#8221; which are checked by an external type checker, such as PHPStan or an IDE, but as comments they are not enforced by the runtime.<\/p>\n\n\n\n<p>PHP 7.0 added the ability to specify primitive types in function parameters and return values &#8211; they are called &#8220;type declarations&#8221;. There are two possible operating modes: &#8220;strict mode&#8221;, where an error is produced if the value does not match the declaration, and the default mode, where a value is implicitly converted to the declared type. PHP 7.4 extended the type declarations to class members. However, the range of supported types in type declarations was extended gradually, and it still isn&#8217;t possible to specify all possible types in type declarations:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Support for the <code>object<\/code> type, without a particular class, was only added in PHP 7.2.<\/li>\n\n\n\n<li>Support for the <code>null<\/code> type, as a standalone type, was only added in PHP 8.2.<\/li>\n\n\n\n<li>It is still not possible to declare a function to accept a particular <code>resource<\/code> type, although the plan is to deprecate the <code>resource<\/code> type altogether by class types defined in the library.<\/li>\n<\/ul>\n\n\n\n<p>There are also some other pseudo-types usable in type declarations, which do not correspond to a real type:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>true<\/code>, <code>false<\/code> &#8211; the pseudo-type <code>false<\/code> was added in PHP 8 to indicate that a function can accept or return <code>false<\/code> in addition to the other specified type, however, they, and the pseudo-type <code>true<\/code>, can only be used standalone since PHP 8.2. It is not possible to specify any other specific values apart from <code>true<\/code>, <code>false<\/code> or <code>null<\/code> (a unary type).<\/li>\n\n\n\n<li><code>void<\/code> &#8211; a pseudo-type which specifies that a function does not return a useful value. Under the hood, <code>null<\/code> is actually returned, but it is a syntax error to specify a value in the return statement in a <code>void<\/code> function, and IDEs can produce a warning when a return value of <code>void<\/code> is used.<\/li>\n\n\n\n<li><code>never<\/code> &#8211; a pseudo-type which contains no values, called the &#8220;bottom type&#8221; in type theory. It indicates that a function does not return normally.<\/li>\n\n\n\n<li><code>self<\/code>, <code>parent<\/code>, <code>static<\/code> &#8211; used to specify relative classes in the class hierarchy.<\/li>\n\n\n\n<li><code>callable<\/code> &#8211; a pseudo-type which represents anything which can be called. Internally it can be either a \n<ul class=\"wp-block-list\">\n<li><code>string<\/code>, representing the name of a function &#8211; before closures were added in PHP 5.3, we had to define a named function to pass it into higher order functions such as <code>array_map<\/code><\/li>\n\n\n\n<li><code>array<\/code> with the <code>object<\/code> at index 0, and the name as a <code>string<\/code> of the method to be called at index 1, representing a method to be called on a particular object<\/li>\n\n\n\n<li><code>object<\/code> containing a method called __invoke, including <code>Closure<\/code> objects resulted from anonymous functions.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><code>mixed<\/code> &#8211; a pseudo-type which encompasses everything, the top type in the type theory. PHPStan by default suppresses type checking on mixed types (analogous to TypeScript&#8217;s <code>any<\/code>, or Dart&#8217;s <code>dynamic<\/code>), but at level 9, the only operation allowed is to assigned it into another <code>mixed<\/code> variable (analogous to TypeScript&#8217;s <code>unknown<\/code>, or Dart&#8217;s <code>Object?<\/code>).<\/li>\n\n\n\n<li><code>iterable<\/code> &#8211; a pseudo-type for values usable in a <code>foreach<\/code> loop, i.e. <code>array<\/code>s and <code>Traversable<\/code>s.<\/li>\n<\/ul>\n\n\n\n<p>It is not possible to specify type aliases in PHP.<\/p>\n\n\n\n<p>PHP 8.0 added union types, and PHP 8.2 added intersection types. However, it is still not possible to specify the parameters and return types of higher-order function types or generic types in the language, which makes it pretty useless for declarative programming techniques involving heavy use of higher-order functions such as <code>array_map<\/code>, <code>array_reduce<\/code> or <code>array_filter<\/code>.<\/p>\n\n\n\n<p>Because of the above limitations, PHPDoc, along with its cumbersome syntax, is still needed for the majority of cases involving complicated types, generic programming and higher-order functions, to achieve full type safety by a static type checker.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Type safety in Javascript<\/h2>\n\n\n\n<p>In Javascript, all values are of one of the following types: the primitive types <code>null<\/code>, <code>undefined<\/code>, <code>number<\/code>, <code>bigint<\/code>, <code>string<\/code>, <code>symbol<\/code>, or the <code>object<\/code> type. Javascript uses prototype-based inheritance which a class-based inheritance can be implemented on, and is done in ECMAScript 6. In Javascript, the class of an object is represented by its constructor function, while the class itself a function which can be called to create an instance of the class. <a href=\"https:\/\/github.com\/miklcct\/js-self-reproducing-class\/blob\/main\/index.js\">I actually made a class where an instance of the class is a subclass of it.<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Type declarations<\/h3>\n\n\n\n<p>The current version of ECMAScript does not support type declarations, <a href=\"https:\/\/github.com\/tc39\/proposal-type-annotations\/\">however, a proposal is made to add syntactic support for type declarations, but not runtime type checking<\/a>, like in the case of Python, which allows external type checkers to be used. Currently, JSDoc is used to add type hints as comments for external type checkers including IDEs, like PHPDoc in PHP, therefore, they share the same problems including cumbersome syntax, and non-integration to the language syntax. <a href=\"https:\/\/depth-first.com\/articles\/2021\/10\/20\/types-without-typescript\/\">It is called &#8220;Typed Javascript&#8221; for some people.<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">TypeScript<\/h3>\n\n\n\n<p>The most popular way to add type declaration into JavaScript is called TypeScript. It is a near-superset, <a href=\"https:\/\/stackoverflow.com\/a\/66415046\/272733\">but not a strict superset<\/a>, of JavaScript, which adds type declarations into the language. TypeScript code needs to be compiled into JavaScript before it can be used by a JavaScript runtime, while most JavaScript code can be interpreted as-is by a TypeScript compiler.<\/p>\n\n\n\n<p>When compiling TypeScript into JavaScript, under most circumstances, the type declarations are simply removed, which forms the basis of the type declarations proposal above, although there are some TypeScript-specific features, e.g. <code>enum<\/code>s, which generates Javascript code.<\/p>\n\n\n\n<p>The type system of TypeScript supports the full set of operations you would expect to be able to do, for example, not only can it represents primitive and class types, it can also be used to specify the internal structure of an array type with differing elements, or higher-order functions with generic types, required for type safety when doing functional programming. In fact, <a href=\"https:\/\/medium.com\/@taitasciore\/arithmetic-operations-in-the-typescript-type-system-because-why-not-cfc2253a93c9\">it is possible to do arithmetic using the type system<\/a>, and taking the matter to the extreme, <a href=\"https:\/\/github.com\/microsoft\/TypeScript\/issues\/14833\">the type system in TypeScript is Turing-complete<\/a>, meaning that it can compute everything which is computable.<\/p>\n\n\n\n<p>For example, in TypeScript, <code><strong>Array<\/strong>&lt;T>.<em>find<\/em><\/code> has signatures of <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>find<\/em><strong>&lt;<\/strong>S <strong>extends <\/strong>T<strong>><\/strong>(predicate<strong>: <\/strong>(value<strong>: <\/strong>T, index<strong>: number<\/strong>, obj<strong>: <\/strong>T[]) <strong>=> <\/strong>value <strong>is <\/strong>S, thisArg<strong>?: any<\/strong>)<strong>: <\/strong>S <strong>| undefined<\/strong><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>find<\/em>(predicate<strong>: <\/strong>(value<strong>: <\/strong>T, index<strong>: <\/strong><strong>number<\/strong>, obj<strong>: <\/strong>T[]) <strong>=&gt; <\/strong><strong>unknown<\/strong>, thisArg<strong>?: <\/strong><strong>any<\/strong>)<strong>: <\/strong>T <strong>| <\/strong><strong>undefined<\/strong>;<\/pre>\n\n\n\n<p>In plain terms, the first line means that <code><em>find<\/em><\/code> accepts a <code>predicate<\/code> function, which accepts a <code>value<\/code> of type <code>T<\/code>, an <code>index<\/code> of type <code>number<\/code>, and an <code>obj<\/code> of type <code>T[]<\/code>, and that function acts a <a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/2\/narrowing.html#using-type-predicates\">type guard<\/a> to confirm that the <code>value<\/code> is of type <code>S<\/code>, which must be a subtype of <code>T<\/code>. In addition, <code><em>find<\/em><\/code> also accepts an optional <code>thisArg<\/code> parameter which can be anything. It will return type <code>S<\/code> or <code>undefined<\/code>. The second line is the corresponding version where <code>predicate<\/code> isn&#8217;t a type predicate, in such case, it simply return type <code>T<\/code> or <code>undefined<\/code>.<\/p>\n\n\n\n<p>The only drawback of using TypeScript is that, a build step is needed to compile TypeScript into JavaScript, but for any non-trivial JavaScript projects, a build step normally already exists to transpile \/ minify JavaScript for production use, making the integration of TypeScript trivial.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Server-side JavaScript<\/h2>\n\n\n\n<p>Although JavaScript was initially created to run code in the web browser, it is possible to run it on the server, just like any other programming languages. Node.js is a standalone runtime for JavaScript which can be installed on a server, while Express.js is a framework to run server-side applications with Node.js. As such, it is possible to use TypeScript to write server-side applications which compile to JavaScript.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Although PHP has been enhanced a lot since PHP 7, it still hasn&#8217;t caught up other programming languages in terms of type safety, making it less suitable for complicated modern web applications. Unless it improves drastically in PHP 9, the decline is inevitable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP, like Javascript, started as a scripting language for the web, with PHP for server side and Javascript for client side. They were initially designed to add simple, dynamic elements into static web pages in an era when processing power was just&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-284","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/posts\/284","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/comments?post=284"}],"version-history":[{"count":1,"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/posts\/284\/revisions"}],"predecessor-version":[{"id":285,"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/posts\/284\/revisions\/285"}],"wp:attachment":[{"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/media?parent=284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/categories?post=284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/tags?post=284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}