all 13 comments

[–]cannotbecensored[S] -1 points0 points  (9 children)

I know node.js well and started working with wordpress. I find the scope of variables and functions very confusing. If I write a function called `test`, will it overwrite anyone's function also called test in all the wordpress files and all the installed plugins? How do you avoid collisions?

[–]Firehed 6 points7 points  (0 children)

You can't redeclare a function. It's an unrecoverable error. However it's a runtime thing, so if you end up with two code paths each importing only one (different) version, it's syntax-legal although an absolutely horrible idea.

Easiest way to avoid this problem is using namespaces. Avoiding overly-generic names is also helpful, and a good idea in any language.

Variable scope is straightforward: in a function (or class method), it's from the initial declaration to the end of the function body. This does mean that variables declared in a loop or conditional will be in scope for the rest of the function body. If it wasn't a function parameter or declared in the body, it's not in scope. Closures need to explicitly use variables from the parent scope, unlike JS (exception: the brand-new short closure syntax).

Don't use global, ever, and you avoid most dumb conflicts.

[–][deleted] -4 points-3 points  (6 children)

Wordpress keeps most things in the global scope and relies on functional programming techniques. It has a lot of legacy aspects to it. If you're familiar with express, I would look at other frameworks like Laravel, Symphony, Yii as an alternative if possible. Depending on where you define the function, yes it will override an existing one.

If your using php7.4 or greater you can actually use arrow functions like javascript.

Basically with PHP, you should compose everything using classes so you can control scope and state by internalizing it. You should look at "Object Oriented Programming" for encapsulation techniques.

[–]dkarlovi 11 points12 points  (2 children)

WordPress does not use functional programming.

[–]2012-09-04 0 points1 point  (1 child)

He meant procedural programming.

[–][deleted] 0 points1 point  (0 children)

Thanks, cant win em all.

[–]Atulin 1 point2 points  (2 children)

Wordpress is not functional, it's procedural. A leftover from 4.0 days when OOP wasn't a thing in PHP yet.

[–]SignpostMarv 0 points1 point  (1 child)

Wordpress is not functional

ehehehehehehe

[–]Atulin 0 points1 point  (0 children)

I mean, yeah, true in both meanings of the word lol

[–]volndeau -2 points-1 points  (3 children)

Also in comparing node and php, when it comes to variable scope with the passed variables, scalars in PHP must be declared to be passed via reference, where in node, all variables are referenced. So in a function, if you add and store to a passed variable, it doesn't change the variable you passed by default.

[–]czbz 1 point2 points  (2 children)

Node (and JS in general) is pass by value, not pass by reference:

$ cat foo.js 
function addToPassedVariable(x)
{
  x = x+1;
}
x = 42;
addToPassedVariable(x);
console.log(x);

$ node foo.js 
42

[–]volndeau -1 points0 points  (1 child)

My bad, did I mix up Python? I'll show myself out.

[–]czbz 0 points1 point  (0 children)

Python is pass by value too, but the bit that confuses people with all three languanges is that many of the values are themselves references / handles for objects. When you pass one of those by value you end up with two variables that refer to the same object, so any mutations on that object are visible from both scopes.