I am reading through PHP Objects, Patterns and Practice 4th Ed and on page 41, Constant Properties chapter says:

Constant properties can contain only primitive values. You cannot assign an object to a constant.

But objects are also primitive values according to the official documentation, as shown here: http://php.net/manual/en/language.types.intro.php

  • Is this an error in the book? It wouldn't be the only one
  • Should one try to report it? The book is quite old and I could not find an obvious place to report errors

Hope this question is appropriate for [programmers.se]


Only scalar data (boolean, integer, float and string) can be contained in constants prior to PHP 5.6. From PHP 5.6 onwards, it is possible to define a constant as a scalar expression, and it is also possible to define an array constant. It is possible to define constants as a resource, but it should be avoided, as it can cause unexpected results.

Source: PHP constants documentation (emphasis mine)

The book is not specific enough. I think by “primitive values”, the author meant “scalar values”.

The book was published in December 2013, while PHP 5.6 was released on August 28th, 2014, which explains why scalar expressions, arrays and resources wouldn't be mentioned in the book.

  • I acknowledge the fact the book was written before PHP 5.6, but not sure if that is relevant in my question's context, as I would still have to ask the same question regarding primitives and objects. The book's author obviously puts primitive values and objects in different categories. Agreed that scalar is probably what he meant, however, probably is not good enough (probably) – bg17aw Jun 1 '16 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.