Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the reason why generics cannot be used in PHP

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you what is the reason why generics can not be used in PHP, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

Why can't we use generics in PHP

We'll delve deeper into the situation behind generics and PHP. It is interesting and important to understand why generics are not supported as first-class citizens in PHP.

Let's see.

There are no generics in PHP. This is the conclusion of last year's Nikita. It's not feasible at all.

To understand why Nikita says this, we need to look at how to implement generics. In general, there are three possible approaches; programming languages that support generics mostly use one of these three methods.

The first is called monomorphic generics. Let's go back to the first article in this series, in which I showed an example of this collection:

Class StringCollection extends Collection {public function offsetGet (mixed $key): string {/ * … Class UserCollection extends Collection {public function offsetGet (mixed $key): User {/ * … * /}}

I explained that we can manually create an implementation of the collection class for each type of collection we need. The workload will be huge and there will be a lot of code, but it will work.

Singleton generics do exactly this, but are automatically implemented behind the scenes. At run time, PHP does not know about generic Collection classes, but rather knows two or more specific implementations:

$users = new Collection (); / / Collection_User$slugs = new Collection (); / / Collection_string

Monomorphic generics is a completely effective method. For example, Rust uses them. One advantage is a series of performance improvements because there are no more generic type checks at run time, so these checks are separate before running the code.

But this immediately reminds us of the problem of monomorphic generics in PHP. PHP does not have the explicit compilation steps that Rust does to divide a generic class into several concrete implementations; most importantly, singleton generics do require a considerable amount of memory because you are making multiple copies of the same class, but there are some differences. This may not be a big problem for compiled Rust binaries, but it is a serious problem for PHP code running from a central point (the server); it can process hundreds or thousands of requests per second.

The next option is to materialize generics. This is an implementation in which generic classes remain intact and type information is dynamically evaluated at run time. C # and Kotlin implement generics, which are closest to PHP's current type system because PHP performs all type checking at run time. The problem here is that a lot of core code refactoring is needed to make materialized generics work, and as you can imagine, as we do more and more type checking at run time, some performance overhead will gradually increase.

This brings us to the last option: ignore generics completely at run time. It's as if they're not there; after all, generic implementations of collection classes, for example, can handle all types of input anyway.

Therefore, there will be no problem if we ignore all generic type checking at run time.

Well, it's not that fast. Ignoring generic types at run time-- which, by the way, is called type erasure, as Java and Python do-- poses some problems for PHP.

To take an example: PHP not only uses types for validation, it also uses type information to dynamically convert values from one type to another-- this is the type juggling I mentioned in the first article in this series:

Function add (int $a, int $b): int {return $a + $b;} add ('1x,'2') / / 3

If PHP ignores the generic type of this "string" collection and we accidentally add an integer to it, it will not be able to warn us if the generic type is removed:

$slugs = new Collection (); $slugs [] = 1; / / 1 will not be converted to'1'

The second and more important issue of type erasure-- maybe you're already yelling on the screen-- is that the type is gone. If generic types are deleted at run time, why should we add them?

This makes sense in Java and Pyton because all type definitions are checked before running the code using a static parser. For example, Java runs a built-in static parser when compiling code; what PHP doesn't do at all: no compilation steps and, of course, no built-in static type checker.

On the other hand. All the advantages of type checking that we discussed in previous articles; they are not from PHP's built-in runtime type checker. When PHP's type checker told us there was a problem, we were already running the code. A type error is essentially causing our program to crash.

Instead, most of the added value of type checking comes from static parsers that don't require us to run the code. As long as programmers provide enough type information, they can well ensure that run-time type errors do not occur. This does not mean that there can be no errors in your code, but you can write PHP code that checks statically completely and does not generate any type of error at run time. Most importantly: we get all the static insight when writing code; this is the most valuable part of any type of system and has nothing to do with run-time type checking.

So do we really need run-time type checking? Because this is the main reason why generics cannot be added to PHP at this time: for PHP, validating generic types at run time is too complex or resource-intensive.

The above is all the content of the article "Why generics can't be used in PHP". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report