Zum Inhalt springen

Differences between JavaScript/TypeScript and PHP8

JavaScript/TypeScript

PHP8

object

In JavaScript, the term „object“ is used to refer to the general data type that (a.) represents an instance of a class or (b.) a standalone entity that constrains properties and methods.

a. same like in PHP, instance of a class

b. JavaScript object that stores information similar to an array

//object literal
let person = {
    name: 'Bernd',
    age: 30,
    city: 'Berlin'
}

//new Object()
let person = new Object();
person.name = 'Bernd';
person.age = 30;
person.city = 'Berlin';

// Object constructor, properties passed as arguments
let person = new Object({
    name: 'Bernd',
    age: 30,
    city: 'Berlin'
});

Accessing properties of an object can be done using dot notation or square bracket notation:

console.log(person.name);
//output: Bernd

console.log(person['age']);
//output: 30

Objects in JavaScript are mutable, which means their properties can be modified, added, or deleted after they’re created:

//modify the age property
person.age = 31;

// Adding a new property
person.job = 'Developer';

// Deleting the city property
delete person.city;

In PHP an object is an instance of a class.

array

//define an array in JS

let numbers = [1, 2, 3];
let fruits = ['Apple', 'Orange', 'Banana'];
let mixedArray = [1, 'two', true, {key: 'value'}];

//empty array
let emptyArray = [];
//accessing array elements in JavaScript

console.log(numbers[0]);
//output: 1

console.log(fruits[1]);
//output: Orange

conslole.log(mixedArray[3]);
//output: {key: 'value'}

In Java Script, arrays are a specialized type of object. While arrays have some unique behavior and features compared to regular objects, they are still objects at their core. Internally, arrays are represented as objects with integer-base keys (treated as strings) that are automatically maintained and used as indices.

Arrays have the ability to hold elements in a specific order and use numerical indices to access these elements, which is a characteristic feature that separates them from regular objects.

let arr = [1, 2, 3];
console.log(typeof arr);
//output: 'object'

//adding a property to the array, like with an object
arr.someProperty = 'Hello';

//Accessing array elements and the added property
console.log(arr[0]); //1
console.log(arr.someProperty); //'Hello'

Despite arrays being objects, they have additional functionalities and optimizations tailored specifically for handling ordered collections of data. The primary distinction is that arrays have specialized behaviors, methods, and optimizations focused on managing ordered collections (e.g. „push()“, „pop()“, „splice()“), making them more suitable for working with lists of elements compared to regular generic objects in JavaScript.

In JS objects are used similar like associative arrays in PHP, but unlike arrays, objects in JavaScript don’t guarantee the order of properties and don’t have built in methods like „push()“ or „pop()“ as arrays do.

Integer keys in objects will be converted internally in strings (same as PHP associative arrays).

In TypeScript an empty object „{}“ will have the types „string“ and „any“.

let emptyObject: {} = {};

Alternatively, you can use the „Record“ utility type to describe an empty object with specific keys and their corresponding value types:

let emptyRecord: Record<string, any> = {};

If you want to specify the structure of the object with certain properties and their types, you can define an interface or type alias:

interface MyEmptyObject {
//here are the properties
}

let myObj: MyEmptyObject = {};
//define an array in PHP

//using the array() construct
$numbers = array(1, 2, 3);

//or square brackets
$fruits = ['Apple', 'Orange', 'Banana'];

$mixedArray = [1, 'two', true, ['key' => 'value']];

//empty array
$emptyArray = [];
//accessing array elements in PHP

echo $numbers[0];
//output: 1

echo $fruits[1];
//output: Orange

print_r($mixedArray[3]);
//output: Array ([key] => value)

In PHP an array is just an array, the standalone object like in JS doesn’t exist, an object is an instance of a class.

Similar to JS, in PHP, if you use numeric keys in associative arrays, they behave like string keys internally.

For instance,

$myArray[1];
$myArray['1']; $myArray["1"];

will access the same element.

Unlike JavaScript objects, PHP arrays aren’t limited to storing key-value pairs; they can also act as numeric arrays (indexed arrays) and associative arrays simultaneously.


Author:
Thomas Hezel
email: info@zazu.berlin

    Schreibe einen Kommentar

    Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert