2

I used print_r on some variables of a framework to figure out their contents and what i saw was like this

Array (
   [pages:navigation] => Navigation
   [pages:via] => via pages
   [item:object:page_top] => Top-level pages
)

I thought this was ok because i can create arrays like

$ar=array('pages:navigation' => 'Navigation',
           'pages:via' => 'via pages');

and it would be no problem but when i create this

class student {
   public $name;
   public function get_name()  {
      return $name;
   }
   private $id;
   public function get_id() {
      return $id;
   }
   protected $email;
}

$s = new student();
$s->name="hi";
print_r($s);

I get this:

student Object ( [name] => hi [id:student:private] => [email:protected] => )

here the symbol : is automatically inserted for id indicating that it is a member of student and is private but it is not inserted for public member name

I cant figure out what does the symbol : actually mean? Is it some kind of namespacing?

lovesh
  • 5,235
  • 9
  • 62
  • 93
  • P.Duplicate : http://stackoverflow.com/questions/2908095/what-is-this-in-php – Mob Sep 10 '11 at 20:29
  • @Mobinga: no its not.in that ques its not clear wat the OP is asking for. may be he is looking for ternary operator or something. just the title is same – lovesh Sep 10 '11 at 22:03

2 Answers2

3

In the array, it has no special meaning. It's just part of the string key; an array key can be any string or integer.

In the object, it's just how print_r displays property names for private properties.

In id:student:private, id is the property name, student the class in which the property is declared, and private is the visibility of the property.

Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194
  • any reason that this symbol `:` is used only for private and potected members? does this symbol mean anything more than a separator? – lovesh Sep 10 '11 at 18:45
1

Chances are it's what the script/page is using to separate values in the key of the array. They may use spaces in key names so using a space wasn't acceptable and needed a character that wouldn't normally be found in the value itself.

Much like namespaces in many other languages use the . as a delimiter, that script decided to use : (which is then parsed at a later date most likely and used as its originally intended).

Without seeing the script in it's entirety, this would only be a guess as to implementation.

Brad Christie
  • 100,477
  • 16
  • 156
  • 200