Posts tagged with best practices

Voodoo PHP

If you're interested in some dark magic you should watch this talk by Marco Pivetta on Voodoo PHP. I really like these show-me-the-code type of talks.

We've often seen "magic" code, but how does it even work? Let's explore some arguably bad PHP coding techniques that are actually used in real world libraries to solve problems that would otherwise be a huge burden for all of us.

Read more

The case for maintainable code

Developers and their employers are often at odds over matters like clean or beautiful code and with good reason: neither ships a product or increases sales. Most end users don’t care what the code looks like, as long as the product works and meets their needs. That means that beautiful code goes out the window when the rubber meets the road and crunch time sets in.

The fact of the matter is that framing code discussions in terms of beauty or attractiveness doesn’t help the case for getting code that’s clean. But there’s another way to frame the discussion that makes more sense, and achieves both the goal of writing clean code and meets the needs of most businesses: the concept of maintainable code.

http://www.brandonsavage.net/the-case-for-maintainable-code/

Read more

Join 9,500+ smart developers

Every month I share what I learn from running Spatie, building Oh Dear, and maintaining 300+ open source packages. Practical takes on Laravel, PHP, and AI that you can actually use.

No spam. Unsubscribe anytime. You can also follow me on X.

Sharing controller logic with traits

We've moved our logic into a trait, a trait can be shared between many controllers without inheritance and it's more reasonable to do things with views and redirects here (though still not ideal). This in my eyes is the best solution to the problem, it's what traits are good for and it's quite convenient.
http://tech.graze.com/2015/04/14/sharing-controller-logic-with-traits-in-php/

The article provides a good overview of how the people at Graze came to this solution.

Read more

How many HTTP status codes should your API use?

... it’s important to remember that API design isn’t strictly about the practical implications on client and server software. The audience for an API is the developer who is going to consume it. Per the "principle of least astonishment," developers will have an easier time learning and understanding an API if it follows the same conventions as other APIs they’re familiar with.
https://blogs.dropbox.com/developers/2015/04/how-many-http-status-codes-should-your-api-use/

Read more

Clone your package inside the vendor directory

Dimitrios Savvopoulos, the creator of the laravel-translatable package (which I use in almost every project), shared a very nice tip on how to develop a package while it is installed as a requirement.

If you have write access to a composer package repository, you have the possibility to continue its development while it is installed as requirement in another project. Let's see how we can accomplish this.
http://dimsav.com/blog/9/git-repository-inside-composer-vendors

If you have another, possibly better, way to go about this, let me know in the comments.

Read more

Semantic versioning

Under this scheme, version numbers and the way they change convey meaning about the underlying code and what has been modified from one version to the next.

...

Given a version number MAJOR.MINOR.PATCH, increment the:

  1. MAJOR version when you make incompatible API changes,
  2. MINOR version when you add functionality in a backwards-compatible manner, and
  3. PATCH version when you make backwards-compatible bug fixes.
Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.
http://semver.org/

Read more

Use BackupPC to create daily backups original

by Freek Van der Herten – 1 minute read

My current hosting provider, DigitalOcean, has a great backup service. It takes frequent snapshots of entire droplets and it's very easy to restore the snapshot quickly. Unfortunately backups are only taken once every week. For our clients this is not acceptable. If you only rely solely on the DO…

Read more