John Davidson

Are there PHP librabries which extend builtin types to ease method chaining?

0 comments
Message:


PHP definitely has no issue letting one define custom classes which ease method chaining like $some_entity->filter(predicate = fn($n) => $n===42)->transform()->show().


But what about built in types like array, integer and string? Componere provides an API for composition of classes, monkey patching, and casting. And Runkit7 also enables to alter classes. Thus said altering primitive types to turn [1,2,3]->last() into a valid PHP code might be more complicated.


All the more, with the __call function, it’s possible to add many proxy methods, as exemplified in ArrayObject documentation


Given these are probably well known, are they some libraries out there that already leverage on these facilities to perform common operations, like the previous one, or "1,2,3"->split(",")?


Related resources




My Answer:
>save()->update()->delete(). However, PHP does not have built-in support for extending built-in types like strings or arrays to enable method chaining.

That being said, there are libraries available that provide this functionality. One such library is "FluentPDO," which extends the PDO class to allow for method chaining when working with databases. Another example is the "Illuminate\Support\Str" class in the Laravel framework, which provides a fluent interface for working with strings.

Overall, while PHP itself does not have built-in support for extending built-in types for method chaining, there are libraries and frameworks available that can provide this functionality.

Rate this post

4 of 5 based on 4643 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us