Modern PHP, soft skills, productivity and time management.

Tag: tips (Page 1 of 2)

Arrow functions in PHP

tl;dr

Arrow functions are very easy and I’m just an old man.


I remember that I saw arrow functions in js for the first time. It was pretty hard to understand for me, but I didn’t do anything in js in years, so I just get used to this unknown.

But recently I’m working in a project with PHP 7.4. And guess what. Yup, arrow functions arrived in PHP in exactly this version. So I hadn’t a choice but to sit down and understand this little bastard.

Arrow functions in PHP are fairly simple, yet quite nice, and a little different than ordinary lambda functions.

Continue reading

Adding a new Logger to Laravel

tl;dr

If you want to add a new logger to Laravel just create new service extending `Logger`, inject it via the constructor and you’re good to go!


I’m a Symfony guy, but from some time I have to use Laravel. There is the neverending war between those, but what do. It’s not that bad eventually…

Recently I was supposed to add logs to some command, for debugging purpose. Laravel has its logger as facade but I didn’t want to use this one, I just wanted to log some data to separate log file, debug the command and delete the log. And there is no way I found to create a separate log by using the `Log` facade.

As it turns out, Laravel’s log facade uses monolog, so there is a possibility to access monolog itself and create a new logger. I’ve searched the web for the best solution, some suggest creating a new facade and using it, some suggests creating logger “in place” and use it only in my command. But none of it seems right
Continue reading

[Edit] How to deploy Symfony app with Capistrano 3 on cheap OVH VPS

tl;dr

If you want to deploy Symfony app using Capistrano 3 to cheap OVH VPS then you’ll need to write a simple task to set proper permissions.

Edit: unfortunately, there was still a little problem with permissions and I made deployment semi-automatic.


I have a very cheap VPS in OVH, it costs only about $2 per month. It is perfect for my needs, I have my blog there, I have my friend’s blog as well. And now I want to move my wife’s portfolio from (even cheaper :P) shared hosting to this VPS.

Until now deploy to shared hosting looked like this:

  1. open FTP
  2. go to portfolio directory
  3. move changed files
  4. app/console cache:clear

Nightmare.

Fortunately, I had access to SSH on my hosting (not popular thing) so I could try to automate this process a little bit. But because I already have VPS then the better idea was to move the portfolio there and use some tool for deployment.

The portfolio is written using Symfony 2 framework so the best idea is to use Capistrano. I had some experience with Capifony which is pretty old, unmaintained customization for Capistrano 2 (which is also quite old). I don’t want to use old stuff. So the goal is easy: deploy Symfony 2 app using Capistrano 3 to OVH, cheap VPS.
Continue reading

How to make cool progressbar in Symfony command?

If you’re writing commands in Symfony then you probably know the ProgressBar component. It’s useful tool showing the current state of operations and, more importantly, ETA and used memory. Cool.

If you’ll go to the official ProgressBar component page then at the beginning you’ll see a very cool progress bar, with colors, icons, changing status messages, but… If you’ll look a little further you’ll see a sad, black and white progress bar, that is far from a “promise” made at the beginning 🙁

Heads up! We’ll make it like this 🙂
Continue reading

Keep your logs short with logrotate

tl;dr

If you are developing some application that writes a lot of logs use `logrotate` to keep them short and don’t allow them to reach gigabytes.


I’m developing in Symfony framework on daily basis. And on the development environment, it creates lots of logs. Every request is logged, every command is logged, every event subscriber is logged. And if you’re using some older version deprecations are also logged. On my computer, every refresh in browser creates over 1 MB of logs! My friend gets almost 10 MB…
Continue reading

Squash – simple way to cleaner git repository

I use git for about 3 years. First I knew only how to use it with PhpStorm – `cmd + t` to update branch, `cmd + k` to commit and push changes. To make new branch I used menu and let the IDE do the work. It was fine until I heard about `squash`. I heard that it is often used when you contribute to someone’s repository. Maintainer of the repo often want clean history – if you want to contribute you must make pull requests with only one commit. And you may do it using squash.

It was a bit too much for me at that time: `squash`, `rebase`, `push –force`. No thanks 😉

Some time later a couple of people in my company started to use git more. They started to make squashes, rebases and so on. So if they wanted to use it I had to learn it too.
Continue reading

A few words about Git hooks

Lets talk about Git hooks. You’re using Git, right? No? But… you have to! Seriously, go to git-scm, GitHub or whatever and check it out.

In Git you have 3 types of files: tracked, ignored and untracked.
Tracked ones are simple – they’re just files you’ve added to the repository. This might be your application’s code. Ignored ones are also simple – if you don’t want to have any file you can add it to `.gitignore` file and you will have it locally but not in the repository. This might be any vendors you can easily install and don’t need to store them in your repository.

But untracked files are a little bit more complicated. These are files you’ve just created and didn’t specify what to do with them. Are those useful and should be added, or are they just temporary files or vendors? They might be also autogenerated files and this post is really about them.
Continue reading

How to start developing in Symfony with only PHP installed?

This blog post will contain useful information for those, who want to start developing in Symfony without setting up a server (like apache or nginx) or installing docker.

PHP from version 5.4 has built in server. You can run it by executing in console php -S 127.0.0.1:8000 in your projects directory. When you go to this address (of course you can change the port and 127.0.0.1 to localhost) you’ll see the main page of your project.

Continue reading

PHPyths Buster: A great string performance test! (updated)

I was challenged in my last post to deeper check performance of strings. I think it will be quite interesting to test out some cases and say which way of using strings is the best.

In this post, I will check several string usages in PHP 5.6 and PHP 7.

To simplify testing I created a simple function which gets 2 arguments: an array of callable functions to compare (as many as you want) with names to display in summary, and a number of loops to test the function…
Continue reading

PHPyths Buster: Single quotes are faster than double quotes

As you may know, in PHP you can use single and double quotes for strings. These two lines are equal:

<?php
$str1 = "some string";
$str2 = 'some string';

// $str1 === $str2 returns true

The only difference here is handling variables placed inside quotes. Let’s look at this simple example:

<?php
$name = "John";
$str1 = "Hello there $name";
$str2 = 'Hello there $name';

echo $str1; // gives Hello there John
echo $str2; // gives Hello there $name

As you can see in double quotes variables are processed and in single quotes they aren’t.
But which one should I use?
Continue reading

« Older posts

© 2024 Krzych Jończyk

Theme by Anders NorenUp ↑