/r/PHP
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
Im not sure where else to ask this cause I feel like I'm losing my sanity.
I was working on a branch today writing some minimal PHP. Commit and push and my formatter I use formatted the doc on save. Simply taking a one line function to two and one or two other lines changed in formatting.
I was reprimanded about 2 hours later. Boss telling me that whitespace and line breaks aren't good and I need to disable all my extensions etc so no formatting happens. I actually checked my commit, saw it and thought it was was cleaner so I kept it lol.
This has come up once before and I recommended we setup a linter or prettier etc. and he said no he didn't want to add more tools.
It was then suggested I use a different editor at work with no extensions...
I do a lot of side work and things too so I don't want to constantly be enabling and disabling extensions daily.
Am I crazy for thinking this is ridiculous or am I totally in the wrong here? It seems like such a simple solution to a minor problem and being forced to use a different editor with no extensions to avoid any auto formatting is absurd.
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.
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()!
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 :-)
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!
Hello everyone!
I need your feedback on my first open source project, and why not might some of you contribute xD.
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?
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)
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?
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...
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.
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.
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
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");
?>
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
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:
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
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.
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:
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!
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.
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
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 ;)
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?
I’ve been searching everywhere but couldn’t find much about it. Anyone familiar with this framework? Benchmark is mind blowing!
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).
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
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?