/r/PHP

Photograph via snooOG

Share and discover the latest news about the PHP ecosystem and its community.

Please respect r/php's rules.

Please follow the rules

Releases: Current Releases, Windows Releases, Old Releases

Contribute to the PHP Documentation

Related subreddits: CSS, JavaScript, Web Design, Wordpress, WebDev


/r/PHP is not a support subreddit. Please visit /r/phphelp for help, or visit StackOverflow.

/r/PHP

177,527 Subscribers

15

PHP Map 3.10 - Arrays and collections made easy

The new version of the PHP package for working with arrays and collections easily adds copy on write versions of all sorting methods:

asorted(), arsorted(), krsorted(), rsorted(), usorted() uasorted() and uksorted()

Have a look at the complete documentation at https://php-map.org.

Examples

Map::from( ['b' => 0, 'a' => 1] )->arsorted();
// ['a' => 1, 'b' => 0]

Map::from( ['a' => 1, 'b' => 0] )->asorted();
// ['b' => 0, 'a' => 1]

Map::from( ['b' => 0, 'a' => 1] )->krsorted();
// ['a' => 1, 'b' => 0]

Map::from( ['a' => 1, 'b' => 0] )->rsorted();
// [0 => 1, 1 => 0]

Map::from( ['a' => 'B', 'b' => 'a'] )->uasorted( 'strcasecmp' );
// ['b' => 'a', 'a' => 'B']

Map::from( ['B' => 'a', 'a' => 'b'] )->uksorted( 'strcasecmp' );
// ['a' => 'b', 'B' => 'a']

Map::from( ['a' => 'B', 'b' => 'a'] )->usorted( 'strcasecmp' );
// [0 => 'a', 1 => 'B']

In all cases, the original maps are not modified. The methods are useful if you need to continue to operate on the original map afterwards but be aware that copying large maps takes time and memory and isn't as efficient as in-memory sorting when using the regular methods like arsort(), asort() krsort(), rsort(), uasort(), uksort() and usort()!

Why PHP Map?

Instead of:

    $list = [['id' => 'one', 'value' => 'v1']];
    $list[] = ['id' => 'two', 'value' => 'v2']
    unset( $list[0] );
    $list = array_filter( $list );
    sort( $list );
    $pairs = array_column( $list, 'value', 'id' );
    $value = reset( $pairs ) ?: null;

Just write:

    $value = map( [['id' => 'one', 'value' => 'v1']] )
        ->push( ['id' => 'two', 'value' => 'v2'] )
        ->remove( 0 )
        ->filter()
        ->sort()
        ->col( 'value', 'id' )
        ->first();

There are several implementations of collections available in PHP but the PHP Map package is feature-rich, dependency free and loved by most developers according to GitHub.

Feel free to like, comment or give a star :-)

https://php-map.org

7 Comments
2024/12/04
11:19 UTC

5

Weekly help thread

Hey there!

This subreddit isn't meant for help threads, though there's one exception to the rule: in this thread you can ask anything you want PHP related, someone will probably be able to help you out!

1 Comment
2024/12/02
07:00 UTC

0

Free open source clinic management system built with Laravel.

Hello everyone!

I need your feedback on my first open source project, and why not might some of you contribute xD.

Github repository

13 Comments
2024/12/01
21:30 UTC

7

Anonymous functions don't work on Attribute parameters

I find it weird that you cannot pass an anonymous function to Attributes but I can pass callable.

This works:

#[AttrA('uniqid')]

And this doesn't

#[AttrA(fn() => uniqid())]

If functions can be called, why not allow anonymous functions? Can someone explain to me why it doesn't work?

10 Comments
2024/12/01
17:48 UTC

50

Wishlist for PHP?

Swooning over 8.4, I got thinking..PHP is actually really mature & a joy to code in.

What is on your wishlist for the language? Name one or as many features in order of most desired. I'll collate results here

Mine:

Native/language asynchronous support (:/ @ Swoole v OpenSwoole)

219 Comments
2024/12/01
09:11 UTC

0

On a serious note

What is the future of this stack as full stack web dev?

PHP React.JS MySQL

What more can i add? What can i do to make more money out of it?

11 Comments
2024/11/30
09:43 UTC

7

Supported PHP Versions in Packagist (Nov 2024 Bettergist refresh)

The Bettgergist Collector project has finished analyzing the 414,579 downloadable packages on Packagist.org for the month of November 2024.

This month, I added a comprehensive report SQL for determining PHP version ranges, as per each project's composer.json. I have included the entire exhaustive of version ranges here...

Supported PHP Versions in Packagist (Nov 2024)

I distilled it into a proper report.

Supported PHP Versions (8.1-8.4):

SELECT * FROM report_version_ranges WHERE min_version >= '8.1';
 min_version | max_version | package_count 
-------------+-------------+---------------
         8.0 |         8.4 |         22446
         8.1 |         8.1 |           269
         8.1 |         8.2 |           215
         8.1 |         8.3 |          1434
         8.1 |         8.4 |         22058
         8.2 |         8.2 |            36
         8.2 |         8.3 |           409
         8.2 |         8.4 |          9293
         8.3 |         8.3 |           118
         8.3 |         8.4 |          2424
         8.4 |         8.4 |            22

Only 36,278 (8.75%) packagist packages support the only supported PHP versions. A good 235,803 (56.7%) support at least PHP 8.1. Of those, 222,594 (53.9%) claim to support the latest PHP 8.4.

34,178 (8.24%) do not support anything above PHP 7.4.

26.7% claim to support PHP 5.x, minimally.

171,575 (41.39%) packages have no PHP compatibility info at all in their composer.json, which I find particularly bad form.

1 Comment
2024/11/30
08:05 UTC

0

What if php had a brother like hy(for python) and fennel(for lua)?

These s-expression languages(hy, fennel) complies to their original language with zero overhead, and can be reversed to their original language with a single command.

How useful would it be? I think we should look at clojure hiccup

(ns my-app.core (:require [hiccup.core :as h]))

(defn my-html [] [:html [:head [:title "My App"]] [:body [:h1 "Welcome to My App"] [:p "This is a paragraph of text."]]])

This is way better than writing html(don't forget the zero overhead), but what i like the most about it is the macros and metaprogramming that comes with these languages.

Edit: https://github.com/phel-lang/phel-lang

25 Comments
2024/11/29
18:16 UTC

29

AWS Certification as a PHP Developer: 4 things I learned

Some time ago, I was learning and taking the AWS certification. I thought about looking at the topic from a PHP developer's perspective. I realized a few things we deal with daily at work. Sharing my conclusions and wishing you a great Friday!

https://dailyrefactor.com/aws-certification-as-a-php-developer-4-things-i-learned

11 Comments
2024/11/29
16:37 UTC

48

Exit is now a proper function in PHP 8.4

This may be something you are aware of if you are closely following the PHP development.

There is this very common code snippet used in many code bases:

die(var_dump($var));

This worked prior to PHP 8.4, which is actually invalid given that die() is an alias of exit() and it expects an exit code rather than the output are trying to dump

This miss information was commonly spread in tutorials in the early days:

<?php  
$site = "https://www.w3schools.com/";  
fopen($site,"r")  
or die("Unable to connect to $site");  
?>

source

instead you would have to do:

var_dump($var); die();
// or
var_dump($var); exit();
// funny enough, this still works
var_dump($var); exit;

Thought it was worth sharing in case you've missed this, and you are like me who always used this wrong.

Great to see either way that PHP is evolving in the correct direction and slowly getting rid of these artifacts of the past.

Edit: Formatting

23 Comments
2024/11/29
15:56 UTC

14

Introducing PhpFileHashMap: A PHP File-Based Hash Map

Hi folks! I’ve just released a new PHP library — PhpFileHashMap — that implements a file-based hash map, designed for efficient data storage and management. This library allows you to persist key-value pairs in a binary file, providing low memory overhead and fast access for large datasets.

Some key features include:

- Persistent storage using a binary file

- Efficient memory usage for handling large amounts of data

- Standard hash map operations like set, get, remove, and more

- Collision handling through chaining

- Performance benchmarks of up to 700k read ops/sec and 140k write ops/sec on my MacBook Air M2 💻

This library can be especially useful for:

- Projects with large datasets where memory overhead could be a concern, but you still need fast data access.

- Lightweight solutions that don’t require complex infrastructure or databases.

- Developers who need to efficiently manage key-value data without sacrificing performance!

Github: https://github.com/white-rabbit-1-sketch/php-file-hash-map

update:

Benchmarks

After running performance benchmarks across different storage systems, here are the results for write and read operations (measured in operations per second):

Hashmap: 140k writes, 280k reads (It was 700k earlier, looks like I've changed something. Whatever, or buffer is ended, dunno for now, will investigate)

Redis: 25k writes, 20k reads

Memcached: 24k writes, 30k reads

MySQL with Hash Index: 6k writes, 15k reads

Aerospike: 5k writes, 5k reads

Waning!

This is not a data storage solution and was never intended to be used as one. Essentially, it is an implementation of the hash map data structure with data stored on disk, and its current applicability is specifically within this context. But of course, you can use it as storage if it suits your task and you understand all the nuances.

19 Comments
2024/11/29
15:51 UTC

0

How do you manage PHP dependencies?

Think & Vote !

View Poll

9 Comments
2024/11/29
12:38 UTC

5

AIpi - Universal API client for common AI models

Hey PHP redditors :)

I'm excited to share a new lightweight PHP library I published recently! It provides a universal interface for interacting with common AI models.
https://github.com/skito/aipi-php

This project has been on my mind for a while, and here’s why I decided to build it:

  • 🔄 Consistency: I noticed AI models share similar principles, but they often differ in how inputs/outputs are structured, which leads using different libraries to integrate each.
  • 🐘 PHP needs more AI love: Despite being one of the most popular web languages, PHP hasn't received the same level of attention in AI tooling compared to Python or JavaScript.
  • 🌐 Open-source: I feel somehow guilty being too busy and not contributing to the open-source community enough :)

It's designed for anyone seeking a lightweight library to connect with AI models - not heavy frameworks and packages with numerous dependencies.

Hope you find it useful! Would love to hear your thoughts and feedback!

8 Comments
2024/11/29
09:15 UTC

16

Anyone using the Mediator pattern?

So I've been brushing up on some of the design patterns I don't really use, and of all of them the mediator patterns seems the most obscure (aside from flyweight, because it's generally less applicable in php).

Every example seems to be "a chatroom" or an "airport tower". Also, many of them seem to use the Observer pattern in conjunction with it line they are inseperable, which I don't believe should be the case.

Just wondering if anyone has a better example they've used it for. I'm just tired of the theoretical nonsense used to explain most design patterns.

12 Comments
2024/11/29
09:46 UTC

27

My new PHPStan focus: multi-phpversion support

My new focus area will be improving the #PHPStan story around multi #php-version supporting code. This means focusing on stuff which is different between PHP versions.

If you want to cover your codebase cross several PHP versions, you need to set up a CI matrix with different PHP versions. You also need multiple PHPStan baselines to ignore errors which are only relevant for a specific PHP version. Such a setup brings additional complexity not everyone is willing to deal with.

In my experience most projects set up PHPStan only for a few PHP versions and ignore the rest, which leaves a lot of potential errors undetected.

Let me work on PHPStan to iterate on this use-case so your next PHP version upgrade will be easier and contain less hurdles.

https://staabm.github.io/2024/11/28/phpstan-php-version-in-scope.html

5 Comments
2024/11/29
09:28 UTC

47

Noticed that PHP.net now has much nicer autocomplete. What else is in the pipeline for the website?

Well, the title basically says everything I'd like to know, just want to add a thank you to the people doing the website renovation ;)

10 Comments
2024/11/28
20:21 UTC

8

Lack of ?

Hi folks! Every developer has faced a moment when the right library or utility just doesn’t exist, forcing them to write custom workarounds or hacks. What’s one of those moments for you? What missing tool or library caused you the most pain?

44 Comments
2024/11/28
13:30 UTC

0

Anyone built something using Fomo framework?

I’ve been searching everywhere but couldn’t find much about it. Anyone familiar with this framework? Benchmark is mind blowing!

2 Comments
2024/11/28
12:15 UTC

51

Aspect PHP extension

Hey everyone

I've been working a new PHP extension called Aspect (A versatile name hinting at adding "aspects" or enhancements to functionality). This extension is meant to provide useful language features and utilities for some common tasks (or maybe not so common).

The first feature I added is a `#[Memoize]` attribute that can be added to any function or method call. For those unfamiliar with the term, memoization is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls to pure functions and returning the cached result when the same inputs occur again.

It's also installable through the new Pie installer

I would appreciate any feedback on the extension (and any possible future features that you would like to see added).

https://github.com/SolidWorx/aspect

33 Comments
2024/11/28
10:57 UTC

26

DRY at all costs: The trap of premature abstraction

Hello,
I’ve written my first article about hasty abstraction (with a PHP example). I hope you find it interesting: https://f2r.github.io/en/hasty-abstraction

17 Comments
2024/11/28
09:57 UTC

11

Performance penalty of readonly class/properties

PHPStorm nags me when a property/class can be made readonly. I know that it can be turned off. But I haven't decided whether or not to prefer readonly properties.

Generally I prefer immutable class objects over mutable ones and can see the appeal of readonly classes/properties.

Does making a class/property readonly have any significant impact on performance or other downsides?

33 Comments
2024/11/28
05:35 UTC

89

What are the pros and cons of PHPStorm vs VSCode in a professional setting?

My new workplace uses VSCode and I am struggling to accomodate to it.

I have worked for a long time on PHPStorm and I am also used to VSCode for my personal project, but I feel like PHPStorm is so much more powerful when it comes to, well, PHP.

For those who've tried both, which one did you prefer and why?

133 Comments
2024/11/27
17:14 UTC

Back To Top