This article is from medium—-; Welcome to our php&Laravel learning group: 109256050
Isn’t that a pompous title? That’s right! Although I’ve been working with PHP for many years now, I still can’t put my finger on the best practices or the best tools for doing this, but I’m going to start trying.
I’ve seen a real change in the way developers work with PHP. Not only is the language clearly becoming more mature and robust with newer and improved versions, but the entire ecosystem around it is changing.
Developers are releasing new tools, third-party libraries, frameworks, and articles that define design patterns to make code more elegant and easy to understand. Some are looking for ways to make work (and your life as a developer) more efficient, simple, and fun
I don’t embrace new trends early on, in fact, I only adopt new tools when I’m sure there’s community support behind them and I really feel it will improve my work. What I often do is try to make my code conform to best practices.
For these reasons, I spend time using tools like Composer and PHPUnit. About a year ago, I opened my heart to all these shiny new things.
PSR first, then Composer, PHPUnit, Travis- CI, and a few other libraries and nifty tools. I even started using an IDE (Vim FTW, PHPStrom integrated with Xdebug is a must for robust development flow)!
What is modern?
The web is full of articles about how bad PHP is, how bad your life would be if you had to work with PHP code, how ugly the language is, and just about anything else you can think of!
If you continue to work with legacy code, your life may not be so good, but if you get the chance to work on a new project and have access to all the new tools, you’ll see the new PHP I’m about to discuss.
I have some problems working with PHP every day, but we can’t be blind to the changes that are taking place in the language, community, and ecosystem. There is still a long way to go, but the PHP world has matured.
I started creating an SDK for internal apis for the company I worked for as a pet project and decided to follow best practices. I already do most of them, but I make a few changes in how I do some things. These changes, along with what I learned last year, are the subject of this article, which I call Modern PHP.
I started creating an SDK for internal apis for the company I worked for as a hobby project and decided to follow best practices. I already do most of them, but I have made some changes in the way I do some things. These changes, along with what I learned last year, are the subject of this article, which I call Modern PHP.
Let’s start the workflow
As I said, I am new to this IDE and phpStorm is a great, great piece of software and I fell in love with it at first sight. This is my first and only IDE. This was my first time trying an IDE, and I didn’t even need to try any other IDES.
The combination with XDebug is perfect, PHP namespace resolution, integration with Composer, integration with GIT, auto-completion, code generation, code refactoring, and more.
You don’t have to use an IDE; in fact, this is a matter of personal preference. You should use whatever tools suit your needs – Vim, Atom, Emacs, Bracket, NetBeans, phpStorm, Eclipse, etc. The two main points here are productivity and efficiency. Your IDE/text editor must help you with at least these two points.
The integration debugger is an important feature for me. For writing code for large projects (and indeed for small projects), you must use a compatible debugger. Let’s forget about var_dumps and print_rs. You need to output these variables at run time, analyze the trace stack, and set breakpoints. These things are essential and make development and refactoring easier.
I didn’t even know there was an alternative, XDebug has everything you need. Do you have a few minutes? If you haven’t done so yet, take a moment to install Xdebug and integrate it into your IDE or text editor. Start debugging your code with the right tools.
GitHub is another tool I want you to keep an eye on. The emphasis here is on integration.
There are several tools integrated with GitHub that you should start using. These tools can generate metrics, run tests, run jobs for you, and do all sorts of things in your workflow during continuous integration. Integration is a good reason to start using GitHub.
Dependency management
Another key aspect of the modern PHP ecosystem is dependency management, and Composer is the tool to do that.
Composer is already 5 years old, but in my opinion, a lot of features have only been adopted in recent years. Maybe it’s because I’m not an early adopter or because PHP developers are reluctant to change.
The tool provides a front-end Packagist, which is a PHP package library made up of PHP libraries, projects, and tools whose source code is stored on GitHub (or somewhere similar like Bitbucket).
All of the libraries I’ve discussed in this article, or perhaps one of your projects, can be added to a project using the simple following method.
$ composer require package_vendor/package_name
If you don’t know the publisher of a package, you can search the package to find and install the appropriate package
$Composer Search package_name would be a great tool if used only to manage dependencies, but it does a lot more. Take the time to install Composer and read its documentation.
Use the command line interface correctly
In the PHP world, we have something called “interactive mode” that can be accessed from a terminal by typing the following:
$ php -a
Interactive mode enabled
php >
Copy the code
At this point, you’re in interactive mode and you can start testing something, and it works, but the tool is too unintuitive, because I know how IPython works, so I tried it a couple of times and finally gave it up.
Fortunately there is a cool new CLI (command line interface) and its name is Psysh. Psysh is an amazing tool full of interesting features that can be installed globally or locally using Composer.
To me, psysh’s best feature is embedded documentation. It is very convenient to access the documentation of a PHP function without going to php.net. The downside is that you need to do some additional configuration before the functionality is fully functional.
Once installed, type the following command (I use Debian here, this may not work for everyone) to make it work
$apt-get install php7.1-sqlite3 $mkdir /usr/local/share/psysh
$ wget http://psysh.org/manual/en/php_manual.sqlite -o /usr/local/share/psysh/php_manual.sqlite
Copy the code
The first command is not required, you can skip this step if you already have Sqlite installed. The second command creates a directory to save the document. The third command downloads the document and saves it to the directory created in the second step. Note that all commands must be executed as root user.
Then you can go like this:
Go to Psysh’s website to learn more about the tool