.Net Core for PHP Engineers — Part I

Rudi Rocha
6 min readNov 28, 2021

Learning new technologies these days is becoming cheap and easy. Every day I see, people of all ages, finding some content on the internet and quickly starting to build very interesting applications. Just like those “free-time-hobby-oriented engineers”, full-time engineers get comfortable with a couple of languages and frameworks and get specialized on them (even with the risk of turning to be framework experts rather than software experts). What happens then when we see other people getting specialized on other languages/frameworks and we start to wonder “should I…”? Or even realizing the language/framework we know isn’t really the best for that project?
The more senior you are, the less time and patience you have to go through this. This might eventually happen because your daytime job is paying you to do so or just because you want to have some different training. Either way, in the end, we will end up applying the mindset of our favorite framework to the content we are learning (and sometimes this can go right).

In my previous working place, PHP was the dominant language. After supporting the transition from Silex to Symfony, looking forward to payments-oriented applications, PHP wasn’t our language of choice (dynamic casting, the memory usage per request, request performance, etc) compared with other languages such as Java or C#. Also, managing teams, we always need to think “how will the teams react if we ask them to develop in a different language?” and it’s not an easy conversation. Having a few conversations and “how-to’s” later, we were able to ship a few APIs to production using .Net Core, and the “should I…” feeling of engineers from other teams started to grow. So why not show everyone that, despite a few different things (language and framework topics), using .Net Core is quite easy for software engineers using Symfony?

What is .Net Core

Literally copying and pasting it’s “Free. Cross-platform. Open-source.
A developer platform for building all your apps.” (https://dotnet.microsoft.com/). With this framework, you can build desktop, web, mobile, cloud, machine learning applications. This means that with .Net Core you can do the same thing as with Symfony using C# instead of PHP.
Also, C# is not PHP…
As a (PHP) software engineer, reading C# code can feel a bit odd. If you look to a PHP class, remove the dollar signs ($), and replace the arrows (->) with dots, it will start to become really similar with the C# code.

First things first — Differences between PHP and C#

Code execution

This is probably the biggest difference between both languages.
PHP is interpreted: it means that when you write a *.php file, the PHP service can grab that file, “read” it, and execute the code.
C# is compiled: it means that you can’t just execute the code written on a *.cs file. C# is a high-level language (like PHP) that is then compiled into a CLR (common language runtime) and finally turned into native code during runtime.

Variables

Another difference: Loosely types language vs strongly typed language. PHP doesn’t require to define the variable’s type (even with recent upgrades allowing types to be defined) while using C#, there’s always a variable type.

variables with PHP

Let’s see how the variables above can be implemented in C#:

The first difference we can notice is the missing “$” characters. Instead of using it to declare the variable, C# requires the variable type and then the name. Some nice to see differences:

  • There is a difference using integers, floats, and decimal (double) values.
  • Single or double quotation marks are used for different variable types. In C#, a Char is different from a String.
  • Arrays of data are not dynamic: there’s a fixed size and type.

C# version 3 introduced an approach of dynamic type assignment but differs from PHP when it comes to changing its own value for a different type. Consider the following example

reassign a variable with a different type in PHP
Dynamic type assigned with C#

Using var key word C# will, at compile-time, create the new variable in memory as the type of the first assignment. If the program tries to assign a new value, from a different type, an exception will be thrown like:

: error CS0029: Cannot implicitly convert type ‘string’ to ‘int’

IF Statements and conditions

Both PHP and C# are very similar in many aspects. On the following examples, the only difference is in fact the way we use variables:

If statements in PHP and C#

Loops

This is another topic where PHP and C# are very similar.

While statements in PHP and C#
Do..While statements in PHP and C#
For statement in PHP and C#
ForEach Statement in PHP and C#

Object-Oriented Programming

Both languages can be used in an OOP way. So, how can we create the same class in both languages?

Class in PHP
Class in C#

Producing PHP or C# code is really similar. At this level, just a few things change, such as extending classes or implementing interfaces where the notation is a bit different.
However, C# can even give more flexibility. Notice the code regarding constructors: C# supports multiple constructors.
Still, around OOP, we can’t forget about namespaces. If PHP allows defining the class namespace by using the namespace … ; at the top of the PHP file, C# makes it possibly using:

namespace dotNetForPHPDeveloper{
class Person {
...
}
}

Conclusion

This is it for the first part! With this article, we can see the main similarities between both languages. The next step is to show how can we build a similar project between .Net Core and Symfony Framework! In the meantime, check this article’s code on github

--

--

Rudi Rocha

Software Engineering Manager and Software Engineer | Server Side Trainer | Human stuff as a hobby.