Programming Languages war Python, PHP, Javascrip (Nodejs) "Object-oriented programming (OPP)"

Carlos Rivas's photo
Carlos Rivas

Published on Dec 14, 2020

4 min read

Hey my friends, a new post about this war. I don't know which is better, but I'll try to figure it out. I hope y'all enjoy this series. Here we go!

We're going to figure out how create classes, methods and some things more.

What is OOP?

It is a programming paradigm based on the concept of "objects", which can contain data and code: data in the form of fields (often known as attributes or properties), and code, in the form of procedures (often known as methods). By Wikipedia. ๐Ÿ˜›

We need to use the word "class" for defining a class ;D in these cases I'm going to create a class about a ship, this ship has a name and a crew, you'll see how to declare a class, how to use it and how create methods into the class for added a new crewmate. Exist a method called "constructor", it's not mandatory, but if you want to create a new class with a few parameters when you instances the object.

But we need to see blood or better code.

Python

I told you about the constructor method, in Python you have to use "init", how you can realize, you have to use the word "def" like functions, so it's easy to remember it. In python other thing you need to know is the word "self" this is the way that you can get information into the class. So you can manipulate the parameters or methods into the class that you need with this word.

class Ship:

  def __init__(self, name, crew):
    self.name = name
    self.crew = crew

  def setNewCrewMate(self, name):
    self.crew.append(name)

luffy = Ship("Sunny", ["Luffy", "Usopp"])

luffy.setNewCrewMate("Sanji")

print(f"{luffy.crew}")

PHP

The constructor method in PHP you have to use __construct and if you need to manipulate any method or attribute you have to use "$this" with this signal "->"

<?php

class Ship
{
    public function __construct($name, $crew)
    {
      $this->name = $name;
      $this->crew = $crew;
    }

    public function setNewCrewMate($name) {
      $this->crew[] = $name;
    }
}

$luffy = new Ship("Sunny", ["Luffy", "Usopp"]);

$luffy->setNewCrewMate("Sanji");

var_dump($luffy->crew);

?>

Javascript

The constructor method in Javascript you have to use the word "constructor" and if you need to manipulate any method or attribute you have to use "this".

class Ship {
  constructor(name, crew) {
    this.name = name;
    this.crew = crew;
  }

  setNewCrewMate(name) {
    this.crew.push(name);
  }
}

let luffy = new Ship("Sunny", ["Luffy", "Usopp"])

luffy.setNewCrewMate("Sanji");

console.log(luffy.crew)

Conclusion

  • Structure: they are very similar, change a little for the braces and their own words.
  • Lines: Python 16, Javascript 16; PHP 22

OOP is a good methodology for working every day. ๐Ÿ’–

Would you like that I write about more info about OOP?

I hope you enjoy my post and remember that I am just a Dev like you!

ย 
Share this