Jump to content

constants: Error in the book "PHP Objects, Patterns and Practice 4th Ed"


bg17aw

Recommended Posts

Hi, 

I am wondering if anyone else can confirm this error, and also can comment on whether it should be reported and how:

- page 41, Constant Properties chapter:

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


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

Any thoughts?

Link to comment
Share on other sites

You indeed cannot assign objects to constants, so this is more of a wording issue. What exactly can be assigned to class constants depends on the PHP version. Prior to 5.6, you could only have scalar literals (booleans, integers, floats and strings). Then costant expressions (like 1 + 2) were added. And in PHP 7 you can even have arrays.

  • Like 1
Link to comment
Share on other sites

A small correction: Arrays in class constants are already supported by PHP 5.6. PHP 7 merely added support for arrays in runtime constants (the define() construct).

 

Long story short: Instead of “primitive values”, the book should say “constant scalars (or arrays in newer PHP versions)”.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.