6 ms·
Build command line apps using PHP 7
- carlmr 9y agoWhy?
- deckarep 9y agoMy thought exactly, but it's always nice to see people push the status quo on what an "acceptable" use of a language should be.
- slipstream- 9y agoI've written small command line scripts in PHP before (without any helper libraries). Using PHP for such things is nice when interfacing with web services (JSON parsing, SimpleXML, etc). Perhaps Peachpie, when it reaches production quality, will also help with that, as then calling .NET (and maybe, through that, WinRT) APIs from PHP code will be made easier.
- deleted 9y ago[deleted]
- grouchoboy 9y agoFor the same reason a lot of people says that the use node as backend. Because they must use javascript in the frontend they use it in the backend. If one is using php as a language for a project and that project has related commands and with php your are capable of doing, why not?
- b101010 9y agophp-cli scripts normally don't have an execution time limit (or a memory limit?), which makes them ideal for upgrades/database maintenance/cron jobs/etc while being able to reuse code from the application itself one example: https://docs.moodle.org/22/en/Administration_via_command_line https://docs.moodle.org/22/en/Administration_via_command_lin...
- korzun 9y ago> php-cli scripts normally don't have an execution time limit (or a memory limit?), which makes them ideal for upgrades/database maintenance/cron jobs/etc while being able to reuse code from the application itself ? PHP enforces memory limits out of the box by default. What languages have execution limits on CLI?
- octalmage 9y agoThey're probably referring to web hosts. Many hosts have timeouts for the web, but not the cli. So for long running tasks you write cli scripts. In the WordPress world this is easily done using WPCli and eval-file.
- veeti 9y agoWhy stop there? Forget Electron, build your next desktop application using PHP-GTK! http://gtk.php.net/index.php http://gtk.php.net/index.php
- jsjohnst 9y agoI've written a handful of apps using PHP-gtk (all written more than ten years ago). I'm sure it's improved since then, but I'd still say it's not worth the effort.
- qualitytime 9y ago[COW ICON]
- korzun 9y agoWhat value are you adding here? There are plenty of companies that rely on PHP that also have to work with CLI. I believe they are better informed when it comes to making decisions between handling CLI via an entirely different language and sticking to PHP.
- oscrperez 9y agoI mean, great exercise. But let's stop and think, is PHP the right tool for this job? :P
- megous 9y agoIt is fine, especially if you want cli tool that can do http, database connections, etc. without any external deps or other PHP code and want it quickly.
- qualitytime 9y agoWhat? You think swift would be a better choice? [COW ICON]
- korzun 9y ago> But let's stop and think, is PHP the right tool for this job? :P Let's stop and think. Do you know what the job is or are you calling it in with this comment?
- ourmandave 9y agoPHP is just the first lang. It's part of the Tarsana project which is his framework for writing command line apps in any lang. To quote the Tarsana project git page... The PHP language was chosen simply because I wanted to rewrite my lumen-generators using this framework. Now I start to consider switching to Javascript... because javascript.
- qualitytime 9y agoI feel this is an overkill since PHP provides very well for the command line out of the box. Main reason for commenting is the current (and I'm sure more) comments questioning "why?" or "right tool for this job". Please, HN overlords, please please add a flag (icon could be a cow) and total counts so that I may filter out such comments.
- hamandcheese 9y agoComments already start to fade out as they get downvotes. It's easy enough to skim past them.
- wmichelin 9y agoNoob question (I've been on HN for over 2 years but I suppose I don't have enough "karma" to do so), but how can one downvote posts?
- grzm 9y agoWhile comments can be flagged and/or downvoted, submissions can only be flagged. There's a karma threshold to flag submissions—offhand I don't recall what it is. From the FAQ (https://news.ycombinator.com/newsfaq.html https://news.ycombinator.com/newsfaq.html): > Why don't I see down arrows? > There are no down arrows on stories. They appear on comments after users reach a certain karma threshold, but never on direct replies.
- TazeTSchnitzel 9y agoI routinely use vanilla PHP (no special libraries) for CLI scripts. It's actually fantastic for this. It has a large set of functions that do everything you want, no imports required, and it has C-like syntax with real types and functions.
- qualitytime 9y agoHear, hear.
- revmoo 9y agoAgreed. Also cli apps with Laravel are amazing if you need to schedule jobs.
- VMG 9y agoHow would you compare it to python?
- ComputerGuru 9y agoLike python but easier to dive into for anyone that’s used C before?
- astrodust 9y agoIt's like Python that forgot to shave. For fifty years.
- deleted 9y ago[deleted]
- TazeTSchnitzel 9y agoIt's a similar experience. PHP is slightly more Perl-ish (dollar signs, no namespaces for built-in functions, `or die`, native string interpolation) and has type declarations.
- astrodust 9y agoHow is C-like syntax a plus? I know Perl, C, C++ and PHP quite well, but I'd never, ever call C-like syntax a plus. It's astonishing how feeble modern PHP is, that to comapre strings you often have to use strcmp. In 2017. It's like all the cruft of ANSI C with a whole bunch of $ characters spiked in.
- troels 9y agoSymfony already provides this functionality. I fail to see why I would use this project over the de-facto standard PHP framework?
- ceejayoz 9y agoYeah, this does seem to be largely duplicating symfony/console (which anyone using Laravel also has some experience with, incidentally).
- webNeat 9y agoMy first intention was to extend Symfony/Console and make it more easy to use, but the main pain points were defining and parsing command line args, mocking stdin and filesystem, ...
- qualitytime 9y agoI wouldn't use either as PHP comes with "batteries included".
- ceejayoz 9y agoBatteries are nice, but it's handy to have a breadboard and some wiring. For a super-simple CLI script without interaction, plain PHP's fine, but if you're making something interactive that's prompting, using color codes, logging, etc., a framework's pretty handy - especially if it's for a larger app that's got models and services to interact with.
- ing33k 9y agoshameless plug : I built a small CLI app using PHP for HTTP load testing ( WIP ), check it out https://github.com/vamsiikrishna/vex https://github.com/vamsiikrishna/vex
- SwellJoe 9y agoOne of the things I've hated about maintaining PHP web apps is how reliant on the web interface they usually are; this has thankfully started to change. Drupal (finally) has decent CLI tools. MediaWiki is among the few to have had CLI tools for many years. But, the thing that struck me about this project and the examples is just how verbose PHP has gotten over the years. It is strikingly similar to old Java, in terms of class definitions and the like. I know PHP has also gotten faster and lot of "grown up" language features in that time, too, but some of its charm as a language with strong "whipuptitude" is lost. I'm certain it's a better language than it was a decade or two ago, and it was, frankly, awful by many measures back then. But, it sure does take a lot of typing. So, I like that things like this exist (though I likely wouldn't use it...I really need most of my CLI tools to be standalone with no non-core dependencies). I'd like it if every web developer knew how to make good CLI apps and did so for every web app. I use Perl or POSIX shell mostly for my CLI apps, but PHP makes perfect sense for tools that work with existing web apps (the Drupal helper scripts I've written are mostly in PHP).
- chx 9y agoSigh. drush has been the Drupal shell since 2009 or something and have been amazing for more years than I can count.
- SwellJoe 9y agodrush is great, and I've been using it for a few years. But, module support in drush was abysmal until pretty recently, though (still pretty weak; I'd guess 80-90% of modules have no drush support). And, for a lot of core Drupal stuff that I would think would be obvious (CRUD for arbitrary entities, for example) one has to write one's own drush plugin, and it's often complicated as hell. I love what drush does, but I've frequently been disappointed/frustrated by what it doesn't. That's not to denigrate drush or the developer(s) of drush. drush is/was a remarkable improvement over the pre-drush era. I just think Drupal wasn't built to be driven by a CLI. It isn't API-native, by any stretch of the imagination, nor is it front-end agnostic. So, drush is historically working around rather than working with, and it shows in that most of Drupal is still inaccessible to drush (at least in Drupal 7; I haven't deployed Drupal 8 and probably never will as the migration from 6 to 7 was too painful for me to be willing to ever do it again). So...I think we may just disagree on what "decent" and "finally" means. Also, compare to some other ways to build web apps; most modern web frameworks have pretty obvious ways to build CLI tools, and often are built around standard frameworks for doing so (Yeoman or generators/console in Rails). Drupal being based on Symfony now gets it a lot of that same kind of thing; so, it is recently decent, by my estimation. Others may disagree, of course.
- deleted 9y ago[deleted]
- Cozumel 9y ago..but why? PHP can do all of this out of the box.
- webNeat 9y agoCan you define and parse command line arguments that easy using raw PHP ?
- TazeTSchnitzel 9y agohttps://php.net/getopt https://php.net/getopt
- webNeat 9y agoI know this one, but it's not as powerful as using something like https://github.com/tarsana/syntax https://github.com/tarsana/syntax, especially if the arguments are very complicated like here https://github.com/webNeat/lumen-generators#resource-generator https://github.com/webNeat/lumen-generators#resource-generat... and need validation.
- wmichelin 9y agoYes. http://php.net/manual/en/reserved.variables.argv.php http://php.net/manual/en/reserved.variables.argv.php
- EGreg 9y agoWhen is PHP finally going to have a vibrant ecosystem of epoll-based async IO, like Node? AmPHP ReactPHP Kraken And so on. I want to run a PHP webserver and handle my own requests!
- hnbroseph 9y agoever feel like things are moving backwards?
- pbowyer 9y agoLooks good, there's also Symfony's console library which does a great job: https://symfony.com/doc/current/components/console.html https://symfony.com/doc/current/components/console.html
- jameslk 9y agoPHP is not really a good candidate for something meant to be distributed to other platforms due to the fun fact that a lot of custom configurations that affect PHP's runtime behavior are held in a system-wide ini file. External modules that allow certain features to work also need to be loaded from this configuration file. This makes having your application work consistently across platforms kind of a nightmare.
- TazeTSchnitzel 9y agophp.ini has its problems, but I don't understand this remark: > External modules that allow certain features to work also need to be loaded from this configuration file. What's wrong with this? The modules in question (at lest, most important extensions) are part of the core codebase and will be available as packages on whatever platform you're using, and the package manager should configure them for you.
- jameslk 9y agoThe problem is that the distributed script(s), if the rely on external modules, will have to guess if those modules are available, rather than just bringing them with the script like other language dependency managers provide. For example, with Ruby's gems or Node's npm, if I install a command line package, external modules (native C extensions) will be installed as well.
- jpswade 9y agoHow does this differ from Symfony console? Why would you choose PHP7 vs another language? Why do you need this level of abstraction just for a simple command line function?
- petraeus 9y agoBeen doing this for years using CI and now Laravel 5