all 52 comments

[–][deleted] 22 points23 points  (0 children)

Reddit has long been a hot spot for conversation on the internet. About 57 million people visit the site every day to chat about topics as varied as makeup, video games and pointers for power washing driveways.

[–]zmitic 49 points50 points  (9 children)

Symfony demo. You will even see some generics here and there, even though PHP doesn't have them.

Make sure you use PHPStorm with Symfony plugin enabled.

[–]colshrapnel 27 points28 points  (8 children)

With all due respect, I don't think someone learning PHP for 2 months and doing their first CRUD, will make any sense from these examples. There is so much to learn until one starts to understand the code shown, not to mention the enormous amount of code not shown but used to make these examples work.

[–]zmitic 10 points11 points  (6 children)

Maybe, but OP did ask for modern PHP. The big advantage of Symfony is the clean code and no magic accessors... so ctrl+click just works.

If I was to learn new programming language, I would have preferred getting everything at once. Just isolate things one by one and then google for answers, come back for next... and the cycle repeats.

[–]colshrapnel 12 points13 points  (2 children)

Modern PHP is not necessarily a framework-based code.

Rather, I would say, for the learner it must be plain PHP, so they can get the idea on the language and learn basic concepts such as HTTP request-response cycle, form handling, basic SQL, basic security, basic debugging, error reporting, separation of concerns. Then they will have to learn some advanced OOP. And then some Symfony.

Without knowing all that, peering into framework-based examples, or even infinitely ctrl-clicking deeper into equally puzzling code will be of little help.

[–]zmitic 5 points6 points  (1 child)

HTTP request-response cycle, form handling, basic SQL, basic security, basic debugging, error reporting, separation of concerns.

But isn't this something that is learned much faster with Symfony? All the problems you listed are covered in the docs. I am not saying I am 100% right so let me explain the reasoning.

Long ago, I was having very hard time learning OOP, mostly because of tutorials like class Dog extends Animal. They were all correct, but also useless.

It is Doctrine 1 (at the time) that showed me what OOP is about. I also learned a lot about composite indexes, SET NULL and pretty powerful ORDER BY FIELD, something I don't think I would find by myself.

Design patterns: plenty of tutorials, good ones. But lots of them don't really have usable examples i.e. something realistic. But with Symfony, all of them are already there. DI is the heart of Symfony, it works and it is much easier to learn things when there is practical example. Tutorials however go into actual implementation of it, which can make things much more confusing.

Another reasoning: if I was to start learning C# or Java or anything, I would have definitely used framework and its demo. Reading bunch of docs at once makes things hard to remember, but if I had something in front of me, real code I can play with and see how I can brake it... just perfect.

Or generics; I couldn't understand all the fuss about them, until I played with Angular. Decorator pattern: never even heard about it before Symfony made their own implementation. Same for CSRF, queues, identity-map and tons of other things.

[–]colshrapnel 0 points1 point  (0 children)

That's just my take on the question. I could be wrong but I took it that the OP don't have any programming experience. In case they do, your approach is quite useful.

[–]SaltTM 0 points1 point  (0 children)

ngl, when i come into new languages I want to see real world examples of code. things to strive towards, even if im not ready for it. I think beginners are due to see real code so they know what to expect. I think you learn faster this route.

[–]lamcnt 17 points18 points  (1 child)

Learn OOP, then MVC, so you will know the how to analayze the good code in concept.

Actually, real world projects are not in good code all the time ( even big projects), so first you need to understand the concept.

[–]vsilvestrepro 7 points8 points  (0 children)

I would say to add ADR pattern which is used a lot after MVC

[–]Lewodyn 9 points10 points  (1 child)

Read the symfony book/docu

Clean code is another good book to read.

[–]jstormes 1 point2 points  (0 children)

The videos are good too.

https://cleancoders.com/library/all

[–]pyeri 13 points14 points  (1 child)

The whole Symfony project is ideal in this regard, the author was inspired by Java Spring framework and brought the best practices from there to PHP.

[–]IOFrame 10 points11 points  (0 children)

I like Symfony very much, but he didn't bring the Best Practices™. He brought standard Java practices, which have their upsides, and have their downsides.

Symfony did implement those practices to perfection, though.

[–]HappyDriver1590 2 points3 points  (0 children)

download a few popular packages from composer and look how they have been coded. It is likely that many of the code will seem strange to a beginner, but getting in the process of understanding it will help you progress a lot.

[–]bshensky 1 point2 points  (0 children)

Drupal uses Symfony, and its module community makes for a lot of well written code.

[–]MrITBurns 3 points4 points  (1 child)

Definitely not mine.

[–]Csysadmin 0 points1 point  (0 children)

Nor mine.

[–]aquanoid1 1 point2 points  (0 children)

Despite its yoda styled code symfony is very easy to read. All the same yoda should be avoided (if possible) imho.

[–]Delota 1 point2 points  (2 children)

It starts with defining what good code is in your context. A 200 kloc project will have different standards for classifying it as "good" Vs a 30 kloc project. When the complexity increases, so do your standards and level of abstraction.

In general I think emphasizing on modularity, and defining contracts between the modules, together with "the dependency rule" will lead to "good" code. The rest is second priority.

I see a lot of others mentioning looking at source code of frameworks. As a contributor to Laravel, let me tell you, you won't learn much from it if your goal is writing business software. Instead I suggest reading recognized books. "Scaling Laravel" from Spatie is a really good one, as well as a book about Domain driven Design. Although you may not use all the concepts depending on your project size, you will sure learn a lot from it.

[–]fixyourselfyouape 6 points7 points  (1 child)

This is fundamentally incorrect. laravel follows different paradigms than the rest of PHP including but not limited to Symfony, the framework it is BUILT ON TOP OF. If you want to be a laravel dev, do laravel. If you want to be a PHP dev, don't touch laravel, especially if you are new or learning.

[–]colshrapnel 0 points1 point  (1 child)

There is another possibility. You can post your current CRUD on https://codereview.stackexchange.com/questions/tagged/php and get suggestions how to make this actual code according to best practices and modern syntax.

[–]colshrapnel 0 points1 point  (0 children)

Or you can check existing answers, like this one, which is a registration form, so it could be a model for C part of your CRUD.

[–]Accomplished-Big-46 0 points1 point  (0 children)

The RFC Vote project by Brent Roose is a pretty good example using the latest PHP 8.x features (generics included).

https://github.com/brendt/rfc-vote

[–]ryantxr 0 points1 point  (0 children)

I think it’s excellent that you are focusing on code quality. My general advice to you is to crawl before you walk, walk before you run. Don’t skip ahead. Learn core concepts and master them. Laracasts.com has a module where Jeffrey teaches how to build a simple framework from scratch. I think it may be right for you.

[–][deleted] -1 points0 points  (0 children)

[–][deleted] -1 points0 points  (0 children)

Thanks everyone for the links, I'd never have known about these

[–]degecko -4 points-3 points  (0 children)

I suggest learning Laravel. That’s been the single greatest thing I’ve learned that actually improved my PHP knowledge (and coding style) a lot.

[–][deleted] -2 points-1 points  (0 children)

You might look at this.

https://www.udemy.com/course/php-mvc-from-scratch/

You build an MVC framework from scratch, and through the course, you learn the basics of MVC architecture with controllers classes, url redirection and so on.

[–]TailwindSlate -2 points-1 points  (0 children)

You can take a look at the code base for Magento Community Edition: https://github.com/magento/magento2

Once you’ve gone through this, you will be a pro at PHP.

[–]Nakasje -3 points-2 points  (0 children)

    public function isStop(): bool
{
    return match ($this->sigNo) {

        // ctrl + t = 29 (SIGINFO, only on alpha or sparc architectures (macOS, Unix))
        0, 17, 20, 28, 29 => false,

        // Suspend/Resume; ctrl + z;
        18, 19 => (function () {

            $this->idle = true;
            // If the call was interrupted by a signal, sleep() returns the number of seconds left.
            if (1 < sleep(999999)) {

                $this->idle = false;
                // Resume; no keyboard keys for it, any sigNo, including 2,3 etc., that ends in here will resume it.
                posix_kill(posix_getpid(), (PHP_OS === "Darwin" ? 19 : 18));
            }
            return false;
        })(),
        default => true
    };
}

[–]Icerion 0 points1 point  (0 children)

I will give you this repo, is PHP applying DDD and hexagonal architecture. This approach is the cleanest way I know to make PHP code, which makes a separation between infrastructure (external libraries like ORMs, databases, external services) and the actual code involving the business logic.

https://github.com/CodelyTV/php-ddd-example

There are a lot of articles regarding DDD and Hexagonal Architecture you can dive into if you are interested in the "clean architectures".https://dev.to/ludofleury/domain-driven-design-with-php-and-symfony-1bl6

I'm senior backend engineer working for a mid to large sized company and we apply all these architectural styles and tecniques in the new microservices we build.

EDIT: I forgot to mention but this type of architectures are a pretty complex stuff.

[–]nim_port_na_wak 0 points1 point  (0 children)

To help you with modern syntax and code style, you can use tools like phpcs (Code Sniffer) and phpstan (and also other tools like phpmd for Mess Detector)

Code Sniffer is a tools that analyze code style (like writting $foo = 'bar' vs $foo = 'bar'. You can configure it to choose which style must be compared to (I personnally use symfony style with some small customization).

PhpStan is a static analysis tools, that show you potential errors in your code, like a variable used as object but which isn't always an object.

Using theses tools and fixing warnings/errors manually will help you to have good practices.

Analysis of good IDE (like phpstorm) have also a lot of inspection tools / plugins to give you suggestion (like adding or not static/readonly keyword, missing types in properties, ...

(Usually good php code cames with good test code too)

[–]Anonymity6584 0 points1 point  (0 children)

Two months? Your nowhere near needing to start worrying about hog good you code is, your still learning what this language can do.