DEV Community

Cover image for Python to PHP: A Developer's Journey of Transition
Smit Jethwa
Smit Jethwa

Posted on

Python to PHP: A Developer's Journey of Transition

Background

My journey as a developer took an unexpected turn when I joined a new organisation that primarily relied on PHP for its backend infrastructure. I found myself having to learn a new language. Having had a decent experience with Python and Django, this transition was quite an interesting experience.

Python and PHP, despite their differences, share a common trait - they are both interpreted languages. This means that instead of compiling the code into machine-level instructions, interpreters run through the program line by line, executing each command in real time. While the overarching concept remains the same, the journey from Python to PHP unveils some intriguing distinctions.

Let's delve into my experience as I navigated this transition, highlighting the contrasts and similarities between these two versatile languages.

Installation

Navigating Local Development Environments
Three months ago, I embarked on a new professional journey, joining a vibrant team that had its unique approach to local development environments. In my previous experiences with Python, setting up my local development environment had been a breeze. However, transitioning to PHP was a different story altogether.

For Windows users, the process of installing PHP was notably more complex than Python. I discovered that there were primarily two main methods to get PHP up and running. The team I joined predominantly used XAMPP and Laragon, both of which presented their own set of advantages and challenges.

While Python installations were usually straightforward, it was an eye-opener to see that PHP often required more effort to configure on Windows. In contrast, Linux distributions like Ubuntu came with PHP pre-installed, simplifying the setup considerably.

In this blog, I'll dive into the intricacies of setting up PHP on Windows using XAMPP and Laragon, sharing my experiences and insights as I navigated this essential aspect of my transition from Python to PHP development.

On the Xampp Server, the URL looks like this: http://localhost/project_name/v1/login
Whereas, in Laragon, It’ll be project_name.test/v1/login [Looks good ;)]

Software Design: MVT vs MVC

Laravel, a PHP web framework is based on MVC (Model-View-Controller) software design. In the case of Django, it’s MVT (Model-View-Template)
M - Model: Database (similar to M for Django)
This Model is the central component of this architecture and manages the data, logic as well as other constraints of the application.
V - View: UI, what the end-user will see. (similar to T for Django)
The View deals with how the data will be displayed to the user and provides various data representation components.
C - Controller: Logic (similar to V for Django)
The Controller manipulates the Model and renders the view by acting as a bridge between both of them.

MVT Design

MVT Design

MVC Design

MVC Design

Syntex

(One thing to remember! Syntex changes, Logic remains the same.)
PHP and Python both have different syntax structures.
E.g.

  • Back to semi colons;
  • Declaration of variables.
    • Python: varibable_name
    • PHP: $variable_name (followed by $ symbol)
  • self changed to this.
  • Python has one of the strict rules of Indentation (4 black spaces). PHP doesn't follow this, It has a conventional way of using Curly Brackets to define a code block {}.

Python:

def sum(number1,number2):
    return number1 + number2
Enter fullscreen mode Exit fullscreen mode

PHP:

function sum($number1, $number2)
{
    return $number1 + $number2;
}
Enter fullscreen mode Exit fullscreen mode

The complete tutorial can be found here on the PHP tutorial website: https://www.phptutorial.net/
Also, It is suggested to follow the Style Guide so that other developers can distinguish the names of variables, functions, classes, methods, and Exceptions etc. It provides rules and guidance for a given programming language, meant to ensure consistency from one project and one programmer to another.
I was using PEP8 for Style Guide for Python Code and now, I am following PSR-12 for PHP code.

Terminologies

Python PHP
dist apps (common functions) Services/Helper
User/models.py Models/UserModel.php
User/views.py Http/Controller/UserController.php
User/renderers.py, serializer.py other misc Services/UserService.php / Helpers/UserHelper.php
app_name/urls.py routes/web.php

Basic Directory Structure

BasicDirectoryStructure

Dependencies: Xampp or Laragon

While setting up a project in Xampp, don't forget to make changes in php.ini to install services like MongoDB or 3rd party services. (PECL:: The PHP Extension Community Library one-stop for Extension Library)
Python supports package managers like pip or conda. pip is widely used PM. PHP uses composer. Composer is a tool for dependency management in PHP.
For example:
Python: pip install pymongo
PHP: composer require mongodb/mongodb

Database

Django used SQLite DB by default and PHP used MySQL.
Here, syntax remains the same. Both frameworks work well with SQL Databases.

The problems I faced in the beginning:

  1. Structure of MVC
  2. To understand, inbuilt methods inside Laravel
  3. Debugging doesn't work well for PHP7
  4. I had to re-install Xampp several times due to the unavailability of the port. Xampp uses port 80
  5. Issued problem while changing Virtual Host in httpd-vhosts.conf

Pt 4 & 5 were solved in Laragon.


Furthermore, there is another crucial element that significantly impacts success, and that is the unwavering support provided by the team. I am grateful to report that I have been fortunate to receive exceptional support from my senior leadership team.

For those just starting in a new environment, beyond adapting to the latest technology, a key source of motivation lies in the practice of code review. Code review not only cultivates a collaborative atmosphere and enhances learning, but it also fuels motivation by fostering a strong sense of responsibility and a commitment to producing top-notch, well-structured code. This process encourages a culture of ongoing improvement and excellence within the team.

I can quote a passage from 'The Software Engineer’s Guidebook' by Gergely Orosz that discusses the importance of code reviews for new team members

New Joiners and Code reviews


As I continue my journey as a PHP developer, I look back at my first three months with gratitude for the opportunities that come with change. Along with Python, PHP has been a transformative experience, pushing me to embrace new challenges and expand my horizons in the world of programming. I eagerly anticipate the adventures and growth that the future holds in this new role.


Links which I used and found useful.

Me now onwards: :D
meme_to_use_php

Feel Free to connect with me on Twitter/X -> @jethwa_smit

Images are from respective owners. Framed with the help of ChatGPT

Top comments (0)