Search code examples
phpobjecttypesscalarcomplex-data-types

What really are scalar and compound data types in PHP?


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 :)


Solution

  • But are compound types really objects?

    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.

    [...] but in PHP, does something similar happen? [...] Do they inherit from Object?

    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.

    $object = new ArrayObject(['A']);
    

    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:

    Where can I find more information about this?

    In the PHP manual in the language reference and in the Standard PHP Library (SPL).


    References: