/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

168,962 Subscribers

13

New package for serializing / deserializing PHP arrays as Markdown

https://packagist.org/packages/neckberg/hashdown

Background is that I keep a lot of personal records in .md files, and also had some import / export and web scraping projects, the data for which required some manual review and editing. And it just seemed a lot nicer to do that in Markdown than in JSON or XML or YAML - especially in cases where a lot of the data was actually HTML or some other type of multi-line text content.

I couldn't find any existing libraries that could read an .md file into a PHP array, so I decided to write my own.

This was my first real foray (as mostly just a WordPress theme developer) into writing unit tests, throwing exceptions, and using composer, so suggestions for improvement are definitely welcome!

Not sure if anyone else will find this useful, but figured I'd share it and see. Let me know what you think!

7 Comments
2024/04/19
22:04 UTC

0

Is there a template or boilerplate built with PHP that has login, dashboard, payment system for selling a recurring product?

I want to sell a subscription product (a standalone software for windows) on an annual basis through a website.

User creates an account, pays for the product, gets access to a dashboard where he enters some of his information which is then picked up by the software.

And the end of the annual term a renewal email has to be automatically for renewal.

Is there something like this in the market?

I tried looking up but couldn't find any? Maybe I'm not using the right term - what could a script like this be called in technical parlance?

The personal homepage website About.me would be a good example of what I am looking to build. Or another example would be a hosting service that is purchased which then sends regular bills to customers automatically.

26 Comments
2024/04/19
20:07 UTC

27

Junior, middle and senior php developers

Hi everyone, how do you determine the seniority of a php dev… can’t be just based on experience right?

So, what go-to questions or tests do you use to figure this out?

40 Comments
2024/04/19
11:29 UTC

5

DDEV/Lando and other alternatives?

We're currently using Lando for all our projects but I've had many issues with it randomly breaking and/or missing features like using .env variables in config files, but I have not had a single use case that was completely impossible in Lando. So I was wondering: can DDEV or any other alternative do just as much or more without the issues I experience, preferably with configuration as easy or easier?

Some examples of an unorthodox setups that I've had to work with is a MySQL and Postgres server at the same time and a project with 3 different webservers using the same database, which cannot feasibly be refactored into a better structure.

Besides that, general pros and cons with these services are greatly appreciated!

43 Comments
2024/04/19
09:38 UTC

32

Created a PHP package dedicated to path management, feedback would be welcome!

I really enjoyed using the path.py lib when I was working with files in python, so I decided to try to create something similar in PHP: path-php

The objective here is to create a light, standalone, multi-platform lib for PHP, allowing to handle paths as objects.

Using it allows you to make things like :

$path = new Path(__file__);

$dir = $path->parent();

foreach($dir->files() as $file) { 
    if ($file->ext() === 'html') { 
        $file->rename($file->name() . '.md'); 
    } 
}

Although most of the provided methods are proxies to the builtin PHP methods, it also tries to provide some other commonly required functions that are missing in the native PHP methods, like a copyTree or a recursive rmdir method.

Another objective is to throw proper exceptions when handling files, by explicitly throwing FileNotFoundException or FileExistsException when needed.

Any feedback would be really welcome, thanks!
https://github.com/olinox14/path-php

To install :

composer require olinox14/path-php

/!\ PHP8.0+ required

9 Comments
2024/04/18
22:08 UTC

39

Exploring Go as a PHP Developer: Insights, Experiences, and Comparisons

Hi, I've been a PHP dev for about 5 years now (longer if you count using it as a hobby) and am looking to branch out and try another backend language. It seems Go is pretty popular and I have started checking it out.

I was wondering if you (as a PHP dev) have learned Go and have any opinions about it (from a PHP perspective). Also, if you have, have you made anything with it? How did it go?

Thanks.

84 Comments
2024/04/18
10:14 UTC

53

Does Anyone Still Need PHP 5 Support in 2024?

UPDATE (2024-04-19)

https://github.com/paragonie/sodium_compat/pull/171 -- We've decided to go ahead with a new major version. The original comment is below the line.


We maintain polyfill libraries (random_compat and sodium_compat, which many of you use as a recursive dependency).

Both tentatively support PHP 5.2.4 through the current version (8.4 is coming later this year). This minimum version was specified to ensure our libraries could be used by projects like WordPress.

WordPress requires PHP 7 today.

Does anyone still require support for PHP 5 today? If so, please let us know.

Otherwise, we plan on releasing a new major version for sodium_compat that cuts off support for PHP 5, and sunsetting random_compat (since it's pointless in PHP 7+).

We're aware of Packagist statistics, etc. but that doesn't speak much to the long tail of unpopular projects that haven't made the transfer (esp. if they use an Enterprise OS that backports patches until the heat death of the universe).

We have, previously, explored what a 2.x version of the library may look like. Just using scalar type declarations cut down a lot of the code size.

56 Comments
2024/04/17
23:01 UTC

4

Iterating only IteratorAggregate instead of Iterator

I've been working with PHP iterator classes/interfaces for some time. After a while I've started creating classes to ease my work and remove some of the duplicite code I've done in my projects. When I started to think about what type I should use (i.e. iterable, Traversable, Iterator...) in some of the code I had started to implement I realized that for some specific logic, classes implementing Iterator interface might differ in their behaviour. For example in some cases, where you need to iterate an iterator inside another one of its iteration, the upper iteration would break (the inner iteration would finish the Iterator instance and the upper iteration would end). This led me to an idea that instead of using iterable type for every code referencing iterators in some way (arguments, return types), I would use IteratorAggregate instead. This way, one doesn't have to think about what specific type of iterator it might use. It's true that this can reduce all of the new functionality only to IteratorAggregate instances. This also means that you can't use it upon simple array type values, but this can be solved by having all types as array|IteratorAggregate (this is bit annoying and would be easily solved by having somthing like the proposed [and ignored] IterableFactory type).

So I was wondering how other developers approach this situation? After some thinking I find the ability iterating both Iterator and IteratorAggregate instances bit... confusing. In other languages you can iterate only the types that are pretty much the same as PHP's IteratorAggregate (C#'s IEnumerable or Java's Iterable).

7 Comments
2024/04/17
19:11 UTC

54

Official/Standard way for checking if array is empty

Recently a small disagreement occurred at a code review when my new colleagues used [] === $array for checking if array is empty. I requested a change because I always check for empty array with empty($array) and I have never honestly seen [] === $array used before. I even needed to check if it works as expected.

Their argument was that empty has some loose behavior in some cases but I disagreed because we use PhpStan and in every place it is guaranteed that array and nothing else will ever be passed.

I thought about it and the only objective argument that I could brought up is that it's the only way it was done up to this point and it would be weird to start doing it in some other way. I found this 3 years old post from this subreddit by which it looks like the most preferred/expected way is empty($array).

So my question is: Is there some standard or official rule that clearly states the "best" way to do this? For example PSR standard or standard in Symfony ecosystem or something? Is there some undeniable benefits for one way or another?

edit: user t_dtm in refered post points out interesting argument for count($array) === 0:

it won't require massive refactoring if the array gets replaced with some type of Countable (collection, map, list, other iterable)...

edit2: It seems to me that [] === $array is not futureproof because of collections and \Countable and so on... empty has the same issue. That would point me to the \count($array) === 0 way that doesn't have those problems.

96 Comments
2024/04/17
18:41 UTC

1

Three-point construction or PHPDoc?

Three-point construction or PHPDoc?

We have a three-point construction (...), which allows you to use an unlimited number of function arguments or provide an array decompression (as an example). I have a question about who does it and how. Imagine that we have a function that takes an array of objects as input and processes them somehow. What do you prefer, write an array as an argument and use phpdoc to describe it as an array of objects, or use a three-point construction and immediately specify the object, getting rid of phpdoc?

23 Comments
2024/04/17
14:28 UTC

1

Detecting MITM with a server-side PHP script

In some circumstances a client computer can be rigged with a man-in-the-middle (MITM) “hook” webpages are loaded. (Some employers may install this, as well as Facebook is known to have been doing it via their “free” VPN.)

Say, if someone loads my website in their web browser. My site uses Let’s Encrypt SSL. So in case of such MITM setup, the plugin in the client OS will intercept and decrypt the outbound https traffic, snoop it, then encrypt it back using their own certificate and send it to my web server. From the server side it will look like SSL traffic.

Thus I’m curious if there’s a way to get from my PHP script the hash or some uniquely identifying ID of a certificate that was used for the https transmission and compare it to the unique hash of my Let’s Encrypt SSL certificate? And thus tell if there’s a MITM intrusion in the traffic.

If no, is it possible to detect this some other way?

19 Comments
2024/04/17
10:57 UTC

52

PHP TypeLang 1.0.0 Release

PHP TypeLang 1.0.0 release https://github.com/php-type-language/parser

I tried to collect all the features of the modern Psalm, PHPStan, Phan, phpDocumentor and others in one place and implement an independent parser.

In addition, I organized documentation on all the syntactic features of this "type description language" in PHP, because the documentation for static analyzers does not cover this information well.

I think that this alternative will be more convenient for implementing tools such as Valinor, since it allows you to describe types separately from phpdocs and take out their attributes or configuration files, as is the case with JMS Serializer. In addition, it provides a completely independent (from the built-in PHP types) analysis, unlike PHPStan Parser, and implements more functionality (More than 25000 tests with full coverage of all features of all PHPStan and Psalm stubs, including PHPStorm).

I think because This parser is not tied to either docblocks or PHP itself (like PHPStan) it will be a useful base for the following tasks:

  1. Generating modern PHP auto-documentation.
  2. Generation of Swagger/OpenAPI types (or their alternatives).
  3. Generating TypeScript types (or other languages) from DTO in your project.
  4. Implementation of more modern and high-quality hydrators with custom types (similar to Doctrine, Valinor, Symfony, JMS, etc.)
  5. Perhaps something else? What are your ideas?

Well, and most importantly, it will help in some way standardize the description of types in PHP. For this, I have prepared a special page with a comparison of the capabilities of PHPStan and Psalm: https://typelang.dev/language.html

I would be glad to see your criticism and suggestions for further functionality that you would like to see in future versions of the library.

14 Comments
2024/04/15
20:10 UTC

5

Any published keyboard shortcut maps for PhpStorm out there?

As a long-term holdout on PhpStorm, I've never been about to jive with its default keyboard shortcuts.

As I've plodded along over the past couple years, I've been adding thing to my own "VS Code-ish" setting, but I'd much rather follow someone else's lead.

Wondering if anyone has published a "more reasonable/canonical PhpStorm keyboard shortcuts"?

Or, if not, maybe I could polish mine up and share for further refinement.

8 Comments
2024/04/15
16:57 UTC

0

I'm looking for a secure PHP solution/script that can manage multi-level login/admin access. Any suggestions?

By multi-level I mean it supports a login system that limits access to certain pages or sections of the website. Think wordpress login categories like Admin, Editor etc that hide pages and functionalities.

I am scared to build this on my own due to security risks and loopholes - hence looking for a tried and tested system.

I know I might not find exactly what I am looking for out of the box so anything that is close to what I want will also work.

38 Comments
2024/04/15
14:12 UTC

4

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!

10 Comments
2024/04/15
06:00 UTC

4

Thinking about Aggregates in Active Record

9 Comments
2024/04/14
20:33 UTC

22

What's the scalable (teams) architecture for growing Laravel applications that works with Active Record limitations without making a mess?

I started a new job. It's a Laravel app that is impossible to maintain and they are asking for help. It looks just like the Laravel documentation except for huge classes inside service folders. There's a lot of confusion to what's the validation layer, business rules, assertions, invariants. It gets worse when they use transactions and multiple relationships.

I've read that if you try to use a layered architecture you'll be constantly fighting with the framework and AR.

Policies and Gates do not scale. Looks like they couldn't find a way to apply ACL or RBAC cleanly.

What's the non abstract, written down, architecture that people are using with this framework to be maintainable long term?

15 Comments
2024/04/14
20:21 UTC

32

Deptrac v2 release on the horizon

It took some time to get the next release ready. But here we are on the final stretch.

We changed the way Deptrac will be distributed in the future. Goodbye Deptrac-shim and hello scoped-release.

Same front facing dependencies but distributed as plain php and yes it still ships with a phar.

But because so much changed under the hood we will have some pre-releases before the final v2.

We would appreciate every help with testing the beta -> https://github.com/qossmic/deptrac.

And please feel free to report any bug we have missed.

10 Comments
2024/04/14
11:29 UTC

0

A Laravel newsletter

Hi folks, from time to time, friends or colleagues ask me how I stay updated with Laravel and PHP. I usually recommend checking out Laravel News, following this subreddit, and some accounts on Twitter, but it doesn't always work for them. So, I thought about starting a dedicated Laravel newsletter. While searching, I found that although Laravel News offers a newsletter, it mainly digests its content. Therefore, I've decided to start something new with a friend, The Laralist.
Here is the first edition: https://thelaralist.com/archive/9bc6ef91-dcb2-4259-9006-e68cfa887989
I'd love to get some feedback:
Do you find it helpful?
Is there anything you feel is missing?
Should I continue this project, or do you think it's unnecessary and I should give up?
I would appreciate any feedback you could provide. Thanks!

7 Comments
2024/04/14
10:59 UTC

24

CSV Blueprint: Auto-validate CSV files using precise YAML-defined schemas

Hello everyone,

Recently, I was tasked with verifying a large number of CSV files to ensure they adhered to specific formatting standards, data integrity, and mathematical correctness. Given that these files originated from various services, it was essential to automate the process while simultaneously keeping the documentation current and integrating it directly with the code.

What was important to me was the rigor of the data types and very obvious settings, so that when reading Yaml everything would be extremely obvious even to those who don't know about the utility.

My proficiency in PHP led me to develop the CSV Blueprint utility, which can be implemented as part of GitHub Actions or run independently in Docker or Phar.

Currently, this utility features over 330 distinct validation rules, including aggregation rules, and boasts extensive integrations with continuous integration systems, really detailed documentation, benchmarks, and support for presets to avoid copy&paste.

Also, my personal achievement is ext-parallel multithreading and it passes as strictly as possible all the linters I know.

I want to get the community's opinion and get new ideas. What else can be added?

PS I wish I had such a tool when I was developing marketplaces where I had to import millions of items from dozens of third-party vendors :)

6 Comments
2024/04/12
20:19 UTC

18

Is there a way to find source code of websites/applications using PHP 1/2/3

I want to see how php code looked in version 1/2/3

15 Comments
2024/04/12
15:38 UTC

26

DbToolsBundle: a Symfony bundle to backup, restore and anonymize your data

We just made a simple tool to run a complete anonymization workflow on our Symfony database, and we'd like to introduce it to the community: https://dbtoolsbundle.readthedocs.io/en/stable https://github.com/makinacorpus/DbToolsBundle

Feel free to test, comment, report issues, we are looking for feedback !

4 Comments
2024/04/12
09:51 UTC

Back To Top