DEV Community

Discussion on: Node vs PHP

mjhd profile image
mjhd

This is a point of great contention, it really depends on how broadly you define an object, I agree that native classes are a mandate for OOP, which is why PHP is actually object oriented. The question to me is, “is object oriented programming SIGNIFICANTLY different than a custom data structure” - any language can add objects in the abstract, but the point is fidelity of class to struct comparison with respect to the language...

Thread Thread
stewieandro profile image
Stuart Slaugh

Javascript is not OOP. When Mr.Stejskal claims everything is an object with methods & properties, he demonstrates the fact that a lot of very good developers know very little about JS. Yes, almost everything is an object, but the semantic meaning of that word is quite different. JS objects are very loosely organized properties and/or methods. Data hiding is possible, but it's not simply a question of declaring a property private. You have to build that feature yourself with closures. Because it is much more accurate to compare JS to Lisp or Scheme! Most problems in JS are solved by combining discreet functions. You can write performant efficient code without using any OO patterns whatsoever. Prototypes are NOT equivalent to, e.g. Java objects. You do not instantiate an instance of an object with a class. There ARE no classes in JS. Object literals can in fact modify their prototype, and this change becomes available to all objects that share that ptototype. Prototypes basically describe the capability of a particular object. They are much closer to function declarations than traditional objects. The 'new' operator and constructor functions do not behave like they would in Java. As Mjhd stated, that's syntactic sugar aimed at OO developers.

Thread Thread
xroal profile image
Roman Stejskal

We're arguing that PHP, which does not even have first-class support for classes (you have to use strings to pass them around, and ::class just expands to a string) is somehow more OO than JavaScript. PHP, where primitives have no attributes or methods, is more OO than a language in which basically everything is an object.

Take a look at Smalltalk, designed by Alan Kay, who coined the term "object-oriented programming". In Smalltalk, everything is an object. Number, string, file, compiler, you name it, it's an object. In fact, a class is an object which creates instances. Right.

You also don't write typical classes like in C++, Java or C#. Instead, there is an Object object which accepts a subclass message in which you describe how instances of that subclass will look like. That description itself is an object of the Metaclass class.

Smalltalk has single inheritance and every class inherits from a superclass.
The Integer class inherits from Number, which in turn inherits from Magnitude, and Magnitude inherits from Object, which is the root object. Much like prototypes in JavaScript, eh?

Also, classes, being just objects, can accept messages! There's no new MyClass construct, you send a message to the class object asking it to instantiate and return a new instance of itself.

Heck, even our well known if condition breaks OO design. In Smalltalk, a comparison like a < b gives you an object of the Boolean class which has ifTrue and ifFalse methods. There's no usual if (condition) { code } else { code } construct.

The class keyword as we know it from C++, Java, C# or PHP is not a mandate of object-oriented language. Using objects that communicate via messages and nothing but that is a mandate for object-oriented language.

PHP fails at this, and so does JavaScript. Neither of them is fully object-oriented, obviously, but I dare to say that JavaScript is much closer to what an object-oriented language looks like than PHP.