PHP5.6 Const new Attribute definition class constants can use constant scalar expressions (Constant scalar expressions), for example:
<?php
Class MyTimer {
Const SEC_PER_DAY = 60 * 60 * 24;
}
?>
The difference between define and const is that define can be used to define global constants, and const is a constant that defines the class.
The difference between static and Define,const is that static-defined variables can be changed, then both are not, and static static variables are initialized directly in memory with the class, and can be used directly, such as $oneclass::hobby.
Can define define an array? For example define (' A_array ', ARRAY (' O ' => ' ooo ', ' x ' => ' xxx ')).
Not before PHP5.6, but you can serialize the array by serialize, such as:
# define constant, serialize array
Define ("FRUITS", Serialize (Array ("Apple", "cherry", "banana"));
# Use it
$my _fruits = unserialize (fruits);
After PHP5.6, you can define an array directly by const:
Const Default_roles = array (' Guy ', ' development team ');
Or:
Const Default_roles = [' Guy ', ' development team '];
If it is PHP7, you can define the array directly with define:
Define (' Default_roles ', array (' Guy ', ' development Team '));