Combining Golang and PHP can solve real-world development challenges
blog.spiralscout.com
blog.spiralscout.com
If you're afraid of something as experimental as Swoole, more stable options do exist, such as ReactPHP. Not quite as performant apparently, but seem to be popular.
In general, I find that the PHP ecosystem is still thriving. It's definitely one of the easier languages to get up and running with, and now thanks to the community, it's imbibing some of the more advanced features found in more modern languages.
I still love PHP, as much as the 10 year old me did when I started using it 20 years ago. It just gets the job done, even if it is a bit ugly at times!
Also, I believe swoole is used extensively by tencent/wechat, so we're in reasonable company!
Sums up what is still one of the major issue-slash-weirdness of the php dev world. For all the frameworks and language advance, despite xdebug existing for over 15 years and working great in pretty much any editor or IDE, most PHP dev including the "experienced" ones have no idea how to use a debugger, or if they do they don't apply it to PHP.
Can you imagine debugging your C/C++/Rust/Go/... programs by adding printf everywhere and re-run the whole thing ? Again and again to find which variable fail; while a simple step by step with watches would solve it in a single run, in a tenth of the time, without needing to crappify the source ? That's what 99% of PHP dev still do.
Even the javascript world have adopted proper debugger right in the browser. But in PHP it's so freaking terrible that when people need to solve a bug you don't hear "step until you spot the issue" but "var_dump all the things" or "log to the symfony debug bar !".
For webapps in particular there can be a lot of value in constantly reloading the page. For example it can allow you to more often visually inspect the page for ui/ux/performance improvements. Particularly in early stages of a user interface where you're still figuring things out.
Yes, absolutely. This is my principal and nearly-exclusive method of debugging the distributed systems I work on all day.
I worked at another company where we used a different kind of Smalltalk serialization to save the GUI's state in an auto-generated error email or log entry, so we could open the application in the exact same UI state when dealing with a bug.
I also barley use a debugger, even for code that does not run in parallel, mostly because I'm used to make logging calls at the right places so that I can find issues way faster than with a debugger (most often I'm only run into hard issues or issues where a debugger will fail, i.e. build scripts -_-)
Then again, it's generally pretty easy for me since I develop with liberal use of debug messages and dumping of data structures at multiple locations depending on the value of the DEBUG environment variable. In production I just run without a DEBUG environment prefix and nobody is bothered by the verboseness, but if there's a problem, step one is generally just doing a test run with DEBUG=3 or something prefixing the command. It's a bit different for web-based scripts like in PHP, but not much so (just make it a param), as long as STDERR goes to a log file.
But if I'm working on a project with components of my choosing, I'm free to make liberal use of PHPUnit, both for unit testing and heavier tests that involve the whole stack. Debugging isn't as smooth as debugging JS or typescript so I lean more on logs and object dumps. Might be different if I used PHPStorm but that feels sluggish to use - says something when I use Emacs with tonnes of packages.
It is possible to do higher quality programming in PHP but WP isn't a friendly environment for that. And environment goes a long way.
I have largely stopped coding in PHP, but I did use xdebug when necessary. I have very little idea of other developers' workflows, but I am very skeptical of this claim.
Zero of them used xdebug until I forced them to.
xdebug is not always the most straightforward thing to set up. I don't tend to bother with it unless I am doing a lot of PHP development (which fortunately has not been the case recently). I've also had less cause to use it since the introduction of the Psy shell. It's still something that I would consider an essential tool, and I do wonder at the circumstances which allowed these teams to be able to get by without it, but the experience could maybe be a bit smoother. I did not think that the ST2/ST3 or Atom integration was that great; the best experience I had was with NetBeans, but NetBeans is otherwise pretty clunky. Presumably your mileage varies somewhat.
I agree that xdebug is hard to set up. I only had reproducible success with it after Vagrant became mainstream. Otherwise we had to share a dev server with all the team members in their own directories, and getting xdebug running on the dev server was a trial-and-error headache.
Debugging is one of the reasons I can’t recommend PHP to anyone for any purpose anymore.
People generally find setting it up confusing (which it can be).
Also xDebug can't discriminate if you have multiple projects running in one fpm etc which can be irritating if your request hit many of them (ie calls that can't be mapped in your debugging session).
I'd say 90%-95% of the time that's entirely sufficient.
"The most effective debugging tool is still careful thought, coupled with judiciously placed print statements." - Brian Wilson Kernighan
IMO the ability to easily diagnose and debug issues is a feature of the code/application and should be taken into account when writing it. Having to fall back on a debugger means your dealing with something not written with maintenance in mind.
You can easily replace dozens of runs of a buggy program with a single debugging session. It's genuinely shocking that anyone would argue that debug mode is better.
Reminds me of the classic old debate of command line vs GUI app. GUI might be easier to run, but command line apps are easier to automated, and overall better suited for power users.
There's also the problem that printf affects output for web programs and therefore isn't feasible if you want to eventually see what the output is.
It's like you have a sick patient and want to see how they're doing by cutting them in half. X-rays and scans make far more sense.
Time difference between printf() & F5 vs. setting up breakpoints and watch variables in a debugger is going to be negligible in something like Ruby/Python/PHP.
Also, everything is so damn framework-heavy these days that stepping until you strike gold can take a very long time. Super-verbose logging would probably be more effective.
It's a shame the article does not explain what was unsatisfactory about php-pm [1], which is a native php solution that also allows the application to boot just once for all requests.
My solution:
Use a Go FastCGI client library to call PHP by talking directly to php-fpm. It saves on the HTTP request overhead and no need to run a web server. Actually, php-fpm is a decent application server itself.
Edit: Here's a link to example code: https://github.com/tomasen/fcgi_client
Since you're just making garbage, it doesn't need to be pretty, it just needs to be fast.
Since you're making garbage, it doesn't need to be future proof, it will only be alive for 10ms.
You can make some really fast pages with PHP if you try, but modern frameworks are going to take longer to load than a fast page will take to be finished.
I want to believe this. I want to be hyped. It sounds great. But there's no reproduction scenario. Some claims are also false (php doesn't kill the process to start the processing cycle). Others seem too good to be true - like 40x speedup.
It just smells like "hey, we are cool kids too, look at us, we're advertising using the hype that other kids use!".
If I'm able to reproduce 40x speedup, I'll so gladly eat my words and flame myself.
Fundamentally, it's an idea that works incredibly well - it's amazing that more people don't make use of it....
I don't really understand this use case - is this for PHP developers to eke out some more performance for their web apps?
The goridge library (https://github.com/spiral/goridge) makes a little more sense to me; it's a bridge between golang and PHP via RPC.
I went through the code (https://github.com/spiral/roadrunner) but it doesn't seem like something you can swap into an existing PHP project. Can anyone explain what use case this might be for?
RoadRunner lets you run "workers" (php processes) that perform all the bootstrapping just once, and then receive-process-reply to requests. The use case is to avoid the (possibly expensive, especially with modern frameworks) bootstrapping for every request.
Check the symfony integration example for instance [1]. Everything that's outside of the
while ($req = $psr7->acceptRequest()) {
...
}
is usually run for every request in traditional PHP, whereas here it is only run once when initializing the worker and then that while block reads/executes/replies to each specific request.The problem with this approach is that you now need to be careful not to leave unwanted stuff behind (open files / db connections, static $properties, etc.) that may interfere with future requests, whereas this just isn't possible in "normal" PHP. I would argue that this is one of the features that makes PHP so beginner-friendly.
[1] https://github.com/spiral/roadrunner/wiki/Symfony-Framework
This is awesome ergonomically (because a past request cannot affect a future one in any way), but has terrible performance because all of the PHP code has to be re-bootstrapped for every request (think: read config files, open db connection, setup the injection-container, etc.).
Thus, this thing is like php-fpm but letting you bootstrap once and then just reply to requests within that php-space.
Aside from the "now you have an extra gun to shoot your foot with" issue, the other major difference is that php-fpm can execute any php file, whereas this approach requires a different process for each entry point (aka "front controller" in modern frameworks, aka "index.php").
> "To them, we say “think again.” We believe that the only limit PHP has is the limit you set."
You shouldn't be writing PHP in the first place, and should slowly work to (incrementally, slowly) shift your codebase into any other programming language in existence if it is already written in PHP.
The core language is terribly designed. PHP 7 did not fix this. Even JavaScript is miles better (PHP has a type system equally as bad, if not worse).
Of course, this sentiment is unpopular, because you're never supposed to rewrite anything even incrementally, and Popular Means Quality, and saying otherwise is heresy.
I propose the more radical solution of killing PHP entirely (again, incrementally, slowly), and replace it entirely with the language of your choosing (maybe that's Go).
What was the alternative to using php from 1996 to 2000 if you were running a small mom and pop isp and your customer base was requesting shopping carts, photo galleries, and blog like web capabilities?
In that era, the servers were pentiums of 75MHz.
Back then you primarily went one of two ways, Perl & PHP. PHP was by far the lowest barrier to development. Easy to enable PHP support in webservers, easy to incrementally increase usage in your platform alongside any static HTML.
PHP + mod_php moved away from needing to know what was happening on the server side.
Perl and the other cgi-bin integrations could be complicated, involved, requiring more specific configuration on the server side. Sure, it's not _that_ hard to do, but it does present a barrier to entry that PHP / mod_php handily solved.
More than two possible ways.
As if everything else wasn't worth mentioning.
Back on my little bubble, the options listed by me were much more relevant.
That's just not true. Just one example: PHP will throw runtime errors when (1) an argument type being passed to a function does not match its corresponding declared parameter type and (2) a value being returned from a function does not match the declared function return type. In pure Javascript you can't restrict the types of objects being passed to or returned from a function.
For people new to development especially who may have started with some simple static HTML, just being able to rename a .html file to .php on a dirt cheap web host and have it "just work" is such a low barrier to entry that it is a tremendous learning tool.
People also like to forget that PHP scales down better than any language out there, making it a no-brainer decision for cheap hosting of small and simple sites.
When you start increasing traffic and throughput or building software to run an serious business, of course there are other great options.
Just remember that every language has a purpose even if it's not yours, otherwise it wouldn't exist.
1) Rapid iteration: Edit a file, hit refresh, see your results.
2) Ease of deployment: SFTP your changes, done (obviously you wouldn't work this way for a serious project...)
3) Simple execution model: each request gets its own thread (or process), each request starts from scratch.
PHP 7's type system is worse than Javascript's type system? I can't imagine any developer who has worked with both those languages agreeing with you. It sounds like you haven't done any serious PHP work for the last ten years or so, and your opinions haven't kept pace with reality.
try{
$exceptionType = rand(1,10) > 5?
BadMethodCallException::class
: LogicException::class;
throw new $exceptionType("smartass");
}
catch(BadMethodCallException $typed_1){
file_put_contents('php://stderr', "Bad method, ".$typed_1->getMessage());
}
catch(LogicException $typed_2){
file_put_contents('php://stderr', "Bad logic, ".$typed_2->getMessage());
}
or this: function lieToMe(PHPMailer\PHPMailer\PHPMailer $mailer){
}
or this: $mailStrategy = new MailStrategy();
$mailStrategy("email@example.com","Subject","Body");
Or any of the other actually cool things you get with PHP if you can cope with the language.It's sad to see people who probably can't program a microwave diss an entire language they haven't ever actually used.