I know that an array and an object are composites, because more than one value can be stored in them, while scalars are "primitive" data, i.e. a single value.
But are compound types really objects?
For example, as in Java, almost everything is an object, an Array
, an instantiation of a class
, a Map
etc., but in PHP, does something similar happen? Array
, ArrayObject
, Map
, etc. Do they inherit from Object?
Or are they just "special" objects?
Where can I find more information about this?
Thank you 🙂
2
Answers
To answer this question:
Answer: Objects are one of the compound data types in PHP, but not all compound data types are objects. Each compound data type serves a specific purpose and is used to handle different kinds of data structures in PHP.
More details for beginners:
Scalar data types represent individual values, meaning they can only hold a single value at a time. PHP has four scalar data types:
Compound data types can hold multiple values or a collection of values. PHP has two primary compound data types:
Given the two compound types array and object (Cf. the two structures JSON is built on), then in PHP this is a clear no: array is array (array or associative array/hashmap) and only objects are really objects.
String is a scalar in the PHP nomenclature, not an array. There is no char type.
For Array() (nowadays []) nothing inherits from object.
You can however create objects from classes that implement ArrayAccess, Countable and Traversable to create something array-like which then is an object.
Still though, there is no single superclass, PHP has is_object() test, the instanceof operator, and type declarations support for object.
There are also some interface+class hierarchies in the Standard PHP Library (SPL) and PHP core:
In the PHP manual in the language reference and in the Standard PHP Library (SPL).
References: