365love.php,从Node.js恢复到PHP的感觉如何?

Hello there community!!!!

It's been a long time since I've written my last post because I was working on some projects full time and I didn't have enough time to write a post.

Anyways, recently I had a really pleasant experience of switching back from Node.js to PHP which was incredibly amusing in some ways; So I wanted to share it with you.

PHP

c938999289e6b8deca443df3973c92e9.png

So as you know PHP is one of the weirdest programming languages out there in many ways, people love it and hate it, it powers lots of greatest softwares like WordPress for blogging and WHMCS for hosting management which are open-source and enterprise in respective order and also lots of poor code practices are also done by PHP programmers since it provides almost no structure. So to be said PHP is both a good programming languages and a bad one at the same time.

Why I love PHP?

A good question! And the answer is because it is pretty easier to write a program using PHP thank in any other programming languages and deployment is also almost no effort since almost 90% of hosting services providers support PHP and Apache web server.

Another reason can be that PHP has good ideas about code splitting which is absolutely nothing! It means if you include some PHP file in the other one you will be able to use it with no more actions taken. Not like JavaScript which have to be exported and imported.

Some PHP features are just awesome you might not find them easily in any other language! One of the things that I love the most about PHP is that it supports variable named variables! Imagine you have a loop of $i and you can implement it in a variable name and declare a new variable each time like $my_var_$i which is going to be $my_variable_1, $my_variable_2 and so on! I know you can this using arrays or similar things in other languages but still it is not the same feeling. LOL

Why I hate PHP then?

As I said despite all the benefits PHP provides it come with some costs too! PHP is one of the most used programming languages and that is caused to be misunderstood with lots of people, especially beginner programmers, so the bad code practices go on and on and spreads around the community. PHP is a kind languages that doesn't care much about disciplines but it is being used against itself.

One of the most famous PHP haters' reason is the spaghetti code (which means a code without a proper structure in case you didn't know the meaning) and that is absolutely right to be honest! A PHP code causes into spaghetti code even for a senior developer in some cases too due to its nature. There are lots of tools to prevent this like Composer for dependency management or Laravel as a framework. But let's be honest you can make a wolf wear a sheep skin but it's gonna be a wolf on the inside.

Another thing I hate about PHP is that it literally have no convention for naming! For instance if you want to have a string's length you would use the function strlen() which is quite OK. So you think every function will be in the same naming convention but what about when you want to replace a string with another in a phrase or sentence or anything else? You have to use the function str_replace() and that's when you realize that you've been tricked!

Node.js

01fe5af3c9ff7d4516065ae8488dac68.png

So let's get into Node.js! My beloved programming environment.

As you know Node.js is the environment which runs JavaScript on V8 engine which is used for server-side stuff and so on. JavaScript is my most loved languages amongst all other languages since I'm a web developer and almost 80% of tools that are used for web developing are made on top of Node.js and JavaScript.

Why I love Node.js/JavaScript?

JavaScript is an object-oriented programming languages and almost everything in JavaScript is an object which comes in handy in many ways. I rather split and join string like this:

let myString = "Adnan Babakan";

myString = myString.split("").join(",");

// Result => Adnan, Babakan

than like this:

$my_string = "Adnan Babakan";

$my_string = implode(", ", explode(" ", $my_string));

// Result => Adnan, Babakan

So both of them are the same thing in JavaScript and PHP respectively but the first one looks more clean in my opinion.

Another reason that I love JavaScript for is the asynchronous and synchronous behaviour it shows as you need. In JavaScript you can have event-based functions so they are run if needed and after a certain task is done.

The versatility of JavaScript itself as it is for front-end and back-end it the same time es maravilloso! (It is in Spanish don't judge me XD)

Why I hate Node.js/JavaScript then?

So despite every and each good point JavaScript has it also comes with some issues as nothing in this world is perfect anyways.

One of the most dramatic problems about JavaScript is the callback hell! So this is exactly the same thing which makes event-based functions possible! You might be asking this guy is crazy! He mentioned it in the loving features part and yet mentioned it again in the negative points part? To be honest YES! Callbacks are perfect and really useful but since you want to make sure that you have access to some data you need to always call them in the callback if you have another function which uses the previous data you need to implement the function in the callback which itself might be a callback and so on! In this case your code is going too deep and mostly growing horizontally than vertically since you have to use tab and spacing characters consecutively! Look at the code below:

fs.readdir(source, function (err, files) {

if (err) {

console.log('Error finding files:' + err)

} else {

files.forEach(function (filename, fileIndex) {

console.log(filename)

gm(source + filename).size(function (err, values) {

if (err) {

console.log('Error identifying file size:' + err)

} else {

console.log(filename + ':' + values)

aspect = (values.width / values.height)

widths.forEach(function (width, widthIndex) {

height = Math.round(width / aspect)

console.log('resizing' + filename + 'to' + height + 'x' + height)

this.resize(width, height).write(dest + 'w' + width + '_' + filename, function(err) {

if (err) console.log('Error writing file:' + err)

})

}.bind(this))

}

})

})

}

})

Code is from http://callbackhell.com/ which is a website explaining more about callback hell.

So anyways this code looks damned and crazy but believe me it works perfectly fine! This code might look much better in PHP I believe!

Another problem about Node.js is the node_modules which is explained perfectly in the image below:

699303cd357358c56a13c9c38ef69072.png

This it literally true! Even for a small landing-page you would get something about 100 megabytes of node_modules which aren't going to be need in the production but still are heavy on your computer!

Back to PHP from Node.js

fb321b115d489eba69c29d994f32a178.png

So nevertheless it is time for my experience of coming back to PHP from Node.js to be told!

For some recent projects I've chosen PHP as my back-end due to some deployment limitations and so on.

So my primary IDE is PhpStorm which supports both PHP and JavaScript perfectly along side with HTML and CSS of course and the first thing I did was installing Laravel as my main framework. Believe me or not I've been so much into Node.js and JavaScript that I didn't even see Laravel's officials websites new look until few days ago. PhpStorm recognized Laravel automatically and it was an awesome feeling.

The first thing I was trying to do was to include the libraries I installed using Composer and I was trying to do something like this:

$exampleLibrary = require 'example-library';

So literally I was mixing up JavaScript with PHP and that was hilarious XD.

For a second I was like: Adnan really? PHP was the first programming languages you learnt and this is how you treat it now? I was shocked how I forgot PHP syntax and workflow then I corrected my code immediately.

The second thing I was mistaken was that I was trying to export something to be able to use in another file!

This was a good experience I got from this project despite all the PHP's power. It is awesome to be back to PHP for some projects. But would I stay with PHP again? Maybe YES and maybe NO! This really depends on the projects scale and the projects need but I will not be biased about PHP or JavaScript anyways and would choose either of them if needed and my advice to you also is that choose what ever best fits you or the project. Never let the wrong side of internet guide you about this! Choose wisely about what you need and which of these programming languages can provide you the needed tools easier!

I hope you enjoyed this post and please let me know again if I'm wrong or if you have any other side of view on the comments section below!

BTW checkout my experience of switching from Nuxt.js to GatsbyJS here:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值