DEV Community

Discussion on: PHP For JavaScript Developers

Collapse
 
lokidev profile image
LokiDev

Hey,

So I need to be the mean part here, I'm really sorry :/.

But there are a few "wrongs" in this article and I need to clarify

Syntax

While you CAN write ?> at the end of your files, you really SHOULDN'T. In Javascript there are multiple competing syntax-standards (like the one from AirBNB or the "Standard" standard.), but PHP is more like Python: There are PSR-2 and PSR-12 which describe exactly how standard-compliant code should look like. No more fights over indentation and so on ;).
php-fig.org/psr/psr-12/

Rules 2.2.:

The closing ?> tag MUST be omitted from files containing only PHP.

Emphasis on the "only".

Standards

Btw the next thing missing (maybe you didn't stumble upon it yet) are other references to the PSR. To step up your game, you really should try to understand all of them. But PLEASE refer from PSR-8, as it's the wrong time for now.

Declaring Variables

define doesn't really declare Variables, but compile time constants. They really can't be changed by any means and are like a little chipmunk replacing all the occurences with it's true value. This is the reason why you can't write sth. like:

define(POWERLEVEL, $sayajin->determinePowerlevel());
Enter fullscreen mode Exit fullscreen mode

What you CAN do is simple math:

define(POWERLEVEL, 2*9*500);
Enter fullscreen mode Exit fullscreen mode

Declaring Arrays

Not really about declaring arrays, but you say, that an Array is the only way to create a collection of key/value pairs without importing.
But you really can (and should) use data classes which hold your data and data collection. Like in javascript you can use them in parameters without their content to be copied by whole, but as a reference!

Functions

Nothing wrong here, but keep in mind, that all Objects are implicitly called by reference, while all scalars (int, string, arrays) are copied by value!

function applySomeProperty($someClass) {
    $someClass->bestBuddy = "Kuririn";
}

$unimportantClass = new class {};
applySomeProperty($unimportantClass);
echo $unimportantClass->newProperty; // Kuririn
Enter fullscreen mode Exit fullscreen mode

And while we're here: Since the 7.x Versions PHP supports types!

function someFunction(int $age, string $name): PersonObject {}
Enter fullscreen mode Exit fullscreen mode

You really should use them!

Classes

Not much here, but it's important to know, that you use :: in static context, while -> in private. Additionally there's a difference between $this::$varname and self::$varname.

Collapse
 
ziizium profile image
Habdul Hazeez

She said and I quote:

I'm still new to PHP

Collapse
 
lokidev profile image
LokiDev

So shes even more dependent on not learning sth. wrong.

How is someone supposed to learn and get better if everyone just sugar-coats you?

Thread Thread
 
ziizium profile image
Habdul Hazeez

So shes even more dependent on not learning sth. wrong.

How is someone supposed to learn and get better if everyone just sugar-coats you?

You might have missed the following comments and responses.

This comment:

Friendly reminder for a syntax shockingly difference between PHP and any other language. The dot ( . ) is the concatenation operator!

$hello = "hello";
$hello .= " world!";
echo $hello; // out: hello world!

For more info, check this out: positronx.io/php-concatenate-strin...

Her response:

I knew I was forgetting something, thank you!


Also this comment:

Setters are unnecessary in PHP. Use immutable private attributes and be happy.

Her response:

Thank you for the clarification!


And this:

Nice work Megan, just one thing: The spread operator is only available in PHP 7.4 everything else is awesome

And her response:

Thank you for the clarification! I'll add that in ASAP! 😀

Collapse
 
smoldev profile image
Megan Miller

I really appreciate this comment. Thank you so much! I'm still learning, so this is really helpful. 😊

Collapse
 
lokidev profile image
LokiDev

I really didn't want to be mean. I'm just the direct kind of guy. Another very helpful resource:
phptherightway.com/

This is a lot. Really. One chapter a week should be good enough to get you in the top 10% :).

Another tip: try to use tools like phpstan (or psalm) and php mess detector as early and strict as possible. They really force you to write better :)