Http Hobo

Another blog with web-development tips’n’tricks. | About me

3 posts tagged

php

Xdebug remote debug configuration tip

Sometimes I need to debug things not locally, but on our development server. Server configuration is pretty classic: PHP works with Apache behind Nginx. As a client I use PhpStorm IDE. Xdebug was configured properly and checked 50 times at least. Same with PhpStorm. But it didn’t work.

Solution was simple and actually I hit this underwater rock not the first time. It’s important to understand on how Xdebug works – although it’s being placed on server, it actually acts like a client. A server would be an IDE. When Xdebug is triggered, it gets your IP address (either hardcoded in config or passed in headers – make sure to check this when using reverse proxy!) and port and connects to it. That means that you can set everything up perfectly, but if your IDE is not reachable from outside, you won’t get your debugger working.

First thing to check would be whether your server has allowed outbound connection to your DBGP port (by default 9000). That’s remote side (I’m trying to prevent client-server mess as above I stated XDebug as a client and IDE as a server). Local side – and that was my problem – is that we usually work behind a router, which masks my computer for an outside world. Yes, already know the answer – DBGP port has to be forwarded on a router to the local IP address.

It seems quite simple now, but with every new configuration I often forget about it and spend hours trying to understand what is going on. Hope this tip would be helpful.

P.S. For those working in some public places (cafes, co-working spaces, etc) – I’m not sure how to help you. Please share your recipes in comments.

P.P.S. For teams working in offices – check out on DBGP proxy, PhpStorm supports it.

2017   debugging   network   php   xdebug

Why I don’t like PHP: inconsistency

There are tons of programming languages nowadays. Some of them are low-level, some of them high-level. Some of them have more features and so-called syntax-sugar, some of them – less. Some of them have more extensions and libraries, some of them – less. Some of them are easier to learn, some of them harder. What I want to say is that it doesn’t really matter. The most pragmatic approach is to use the one which you are going to be paid for.

However, good language makes your life easier. Besides all the things said above, it should be intuitively understandable. You might not know how to write some things, but with a good language you would have an idea. This is what in my opinion PHP is lack of.

Consistency is one of the key pillars of intuitive understanding. If you know that string functions always start with str_ prefix, and you are looking into some new function, you would just type str_ in your IDE and wait for autocompletion tips. Seriously, if a function is named well, you would find it. But not in PHP. In PHP string functions start with str_, str or often have no prefix.

Function replacing a substring into something else is str_replace(). Functions trimming garbage are trim(), ltrim() and rtrim(). If you want to know string length, you go with strlen() function. You can’t keep this in mind, you can’t intuitively find needed function, so often you just have to google it.

I remember once my boss told me that the better interface of an application is, the less calls he will have to the support, which leads to less operational costs. From this point of view this PHP naming hell is very unfortunate.

Now let’s take a look on array functions. Naming is better there, most of the functions start with array_ prefix. But don’t relax, it’s not over yet. Let’s say we have an array of some objects and we need to have an array of their IDs. We go with array_map() function:

$objects = array($var1, $var2, ..., $varN);
$ids = array_map(function ($obj) { return $obj->id; }, $objects);

Pretty clear, isn’t it?. Function array_map() receives first parameter as a callback function, second as an array. Now when we have an array of IDs, let’s say we want to filter it and return only those IDs which are less than 10 (I know, it is quite stupid, can’t imagine why I would need to do this, but whatever):

// Now knowing array_map() function you write the following:
$filtered_ids = array_filter(function($id) { return $id >= 10; }, $ids);

Aaaaaand... this will not work! Because array_filter() receives an array as the first parameter, callback should be the second one:

$filtered_ids = array_filter($ids, function($id) { return $id >= 10; });

What the hell? How such inconsistencies would happen?

2017   php   programming linguistics

Toolbox: PHP sandbox and Regex tester

I’ve heard a lot of people using JS sandbox JSFiddle although I’m not much of a frontend developer. But never heard about somebody using any PHP sandbox.

Here I am, and here is PHP sandbox I love to use. It has a bunch of different PHP versions and a convenient code formatter which makes it a great everyday tool.

That’s it. Short post.

In order to make it longer I would share a link to regular expression tester which I also love a lot. I don’t know if anybody know about it, but have never heard a mention about it. This tool is so good that I used it first to understand what the heck are the regular expressions, and then learned there how to write them. Can any other tester or debugger do that?

Which instruments do you use? Share them in comments.

Picture from https://advivalares.wordpress.com/2013/05/14/criancas-ganharao-bercario/

2017   php   regular expressions   toolbox