Namespace: right and wrong between PHP and Node.js

Namespace: right and wrong between PHP and Node.js

I have learned both PHP and Node.js, there are something I want to say according to the design of the concept `namespace` in both of the two languages.

* It is right that Node.js has no `namespace` keyword

When in old times (c or c++), we compile the source files into several object files, and then into the target execute or library. Cause several sources will be compiled into one, they need a separate name to distinguish the components (we name it "name conflicts") when the project goes large. So c++ comes with namespaces.

When PHP adds OOP concepts, it also follows those famous programming languages: it adds the `namespace`. Just like the thing we all know, then there also comes the autoload -- mapping from the `namespace/class` to the source file, so that when you add `use Foo\Bar;` , PHP could know where the class can be loaded.

But PHP itself is a scripting language, the directory actually can be the natural namespace. Node.js does the smart things, it just do `var Bar = require("Foo/Bar");`, everything is fine. Actually, not just the scripting language, as you can see, golang also treats the directory as a natural separator of the components.

* It is right that PHP has two levels in `vendor` directory

The thing is very funny. Because PHP does have `namespace` keyword, and for one author, it is very easily that the guy does two packages and put them into one vendor name. Ex.: if he (me, "Yarco"), did two packages "Foo" and "Bar", and then they will be both saved under "yarco" directory (there exists a mapping from "Yarco" to "yarco"). So actually, the packages are grouped by vendor names, here "yarco".

Then let's turn back to see Node.js, if you've installed, for example, webpack module, when you look into `node_modules` directory, you got shocked. Oh man, what is those? (See the cover image of this article). It has only one level.

Conclusion: in modern programming languages design, we don't need `namespace` keyword anymore, directory can be the natural separator; And the same time, the concept of `namespace` is still there, when thinking about the packages layout, you should take `vendor` names into the account.



To view or add a comment, sign in

More articles by Yarco Wang

  • My First glance at AutoAI in Watson Studio

    First of all, let me introduce myself, I am not an expert in the area of Data Science, and even didn't graduate from…

  • Surface: a new programming language design view

    This article is not going to introduce another programming language, but, a new view on designing a programming…

    2 Comments
  • Evolution of the Open Source development: Using Blockchain

    We’ve once created open source projects for fun or they are just the side projects during working. Those projects may…

    1 Comment
  • Differences between REST&GraphQL -- my view

    Original Post: https://notes.bbish.

    2 Comments
  • Yin- Yang and Coexistence Theory

    Abstract Game theory(see 1.) is discovered by John von Neumann, mainly focus on the rela- tionships between competitors.

    2 Comments
  • html5 - suggestion of new element and attribute `greyarea`

    There are different types of resources in internet, from article to video. Normally, when we publish a resource, we…

  • Why we work 5 days a week

    I always feel unhappy or losing inspiration during a job in some company. And of cause, we need job to live.

  • Sex, Yin-yang And Family

    The basic axiom in natural is everything try to keep balance, like Swing or pendulum, always goes from one side to…

    1 Comment
  • Data structure and society

    When i'm trying to solve the problem (or at least on the first step ): My Question and when i review the book i bought…

Explore content categories