Skip to main content

Command Palette

Search for a command to run...

PHP is funny but...

Updated
5 min read
PHP is funny but...

Currently, I am working on a 500k lines code project. It is strictly structured, OOP, Models, Services, Controllers, and whatnot. Sadly every time we need to make changes to the code, it gets harder and prone to errors.

PHP is funny but in the long run, it can’t compete with strong type languages.

So far, I have to deal with the next problems:

  • Async operations. PHP has some async operations but they are mostly hacky or use a different library that “changes the whole working pipeline” (such as Swoole, which doesn’t work in Windows so it is a big deal for me). So, PHP could do some async tasks but nothing native.

    • Why is it a big deal? Performance. Even if your (cloud) server is single-core, it still could use and enjoy the use of multiple threads.
  • Refactoring. Refactoring is a PITA. Even if you use some library, sooner or later you will need to refactor and it will involve a lot of manual changes. There are some tools but they aren’t foolproof.

  • Frameworks. For PHP Laravel IS-THE-MOST-POPULAR framework (by a long shoot). However, it is not perfect. Laravel is more of a syntax sugar wrapper in some classes, and while it works, it doesn’t bring much of the native functionalities of PHP. And for Code Igniter, it is the same as Laravel but the syntax is more sandy than sweet.

7 Best PHP Frameworks: MVC Frameworks For Web Development

  • Dealing with XML. It is not common but it could happen. PHP is unable to deal with XML. Example:
<root>
    <items>
         <item>cocacola</item>
         <item>fanta</item>
    </items>
</root>
  • PHP will understand it (more or less) as:
{
  "items":[
      {"item":"cocacola"},
      {"item":"fanta"}
 ]
}

So, the field items are understood as an array of elements.

However, if the XML is:

<root>
    <items>
         <item>fanta</item>
    </items>
</root>

Then PHP understood it as

{
  "items":{"item":"cocacola"}
}

So it changes the meaning of the item depending if it has more than 1 element or not. And of course, it can’t deal (without manual changes) with:

<root>
    <items atr=20 atr2="22">
    </items>
</root>
  • And PHP could fail with JSON too. For PHP
{
"items":[]
}

And

{
"items":{}
}

are the same even when conceptually they are different.

  • Lacks of tooling. The best IDE for PHP is PHPStorm, bar none. However, it is not for free (but Jetbrains is giving free alternatives for some developers). The other alternative is Visual Studio Code but it works like a powerhyped notepad than a proper IDE. The best plugin for Visual Studio Code is a paid one, so working for PHP requires some investment (or work without tooling which is insane for a big project).

How we use PhpStorm for WordPress Development

  • Debug features: PHP lacks it. There is a plugin called XDebug but it works (more or less) half of the time. In some cases, it is impossible to properly debug a code without some hacky solution. Most developers use var_dump() & php_error.log file but it is a no-go for a huge project.

  • PHP doesn’t have the concept of Application. It is a worthy feature that could be used in many ways, including caching. PHP could create an Application to do some hacky features.

  • PHP is not memory-friendly. It has some nice memory usage tricks, however:

$a=["hello","world"];
$b=$a; // $b doesn't use much memory because it uses the same memory as $a, so it is nice trick.
// However $a uses (more or less) 32 bytes (it could be more)

The same example in C#

var a1 = new[] { "hello", "world" }; // 16 bytes (more or less, plus some static stuff but you get the idea)
  • In PHP, the more complex the array, then the worse, and it heavily requires the use of lookup.
$customer=[
    "name":"john",
    "address":[
              "street":"sunset blv",
              "country":[
                        "name":"canada"
                       ]
              ]
];
var_dump($customer["address"]["country"]["name"]); // uses 3 lookups.
// do it with an array of 10000 customers and you get why it is a big deal.
  • PHP lacks data types.

    • int

    • float

    • string

    • and boolean

    • (and others such as object, etc.)

Example:

$x = 12.6;
$y= 10;
$z = $x - $y; // 2.6
var_dump( $z == 2.6 ); // false $z shows 2.6 but in reality, it is 2.5999999..

One solution is to round the values but it is not practical for the long round. Another solution is to use an extension. We can use a proper decimal value (instead of a float) adding an extension to PHP. However, since it is not native, then serializing the value is a challenge per se, and most in-build functions don’t work.

  • Classes aren’t tricky and sometimes it doesn’t work as expected. Also, in Laravel, model classes are arrays in disguise under the syntax sugar of magic methods.
class Customer {
   public $name;
   public $address;
}
$customer=new Customer();
$customer->name="xxx";
$customer->address="yyyy";
$json=json_serialize($customer);
var_dump($json); // {"name":"xxx","address":"yyyy"}
var_dump(json_deserialize($json)); // it returns astdclass. 😬
  • PHP is not null safety. PHP 8.2, 8.3. and 8.4 have added new features but nulls could still happen.

What is my conclusion?

Developing for PHP is fast initially (no compile stage is neat) but its a show stopper in the long run.

What is the alternative?

  • C#

    • Pro: multiple platforms (however I won’t recommend MAUI and Xamarin), LINQ, lots of tooling, active community, and free IDE for small companies (Visual Studio)

    • Cons: Microsoft, also Microsoft is using C# to try to sell their online services (Net Aspire, Microsoft authentications nonsense, etc.)

  • Kotlin

    • Pro: multiple platforms, compatible with Java. A huge library is available (because of Java). Lots of toolings, active community, and free IDE (android studio / IntelliJ with some restrictions).

    • Cons: Oracle (but we could use a community runtime machine). Some popular libraries are dusty and we need to let it go (Spring Boot). Also, Kotlin adopted a lot of nonsense from Java. 🤣

What is NOT an alternative?

  • Python: Because it works similar to PHP but with a worse performance

performance level of python and PHP

What is the worst alternative?

  • JavaScript (server size).