Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Does a colon : in an array key have any special significance?

Tags:

arrays

php

Q: Does a colon : in an array key have any special significance?

From the manual:

An array can be created by the array() language construct. It takes as parameters any number of comma-separated key => value pairs.

array(  key =>  value
     , ...
     )

While I was investigating what the Exception Object is set to, I created an error condition and put this line in-

print_r($ex);

Then in the web page produced looked at the source and it produces output like this-

Exception Object

(

[message:protected] => DB connection error: SQLSTATE[28000] [1045] Access denied for user 'test'@'localhost' (using password: YES)

[string:Exception:private] => 

Is the colon : in [message:protected] significant or is the key to the key => value pair literally message:protected ?

like image 387
tur130 Avatar asked Jan 17 '23 10:01

tur130


2 Answers

$ex is not an array but rather an object. This is how print_r is printing it, indicating with :protected that the message field is marked as protected in the Exception class.

The colon doesn't have any special meaning in the arrays.

like image 124
penartur Avatar answered Feb 15 '23 23:02

penartur


$ex is not an array, it is an object. Objects are more complicated data structures than arrays. What you see is a textual representation of the state of this object.

The developers decided to use a similar representation as it is used for arrays, and they use the colon separation for giving further information about the attributes of the object.

The colon has no meaning, and you won't be able to access a field with e.g. $ex['message:protected'].

like image 43
Felix Kling Avatar answered Feb 15 '23 23:02

Felix Kling