Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access object property when property name contains - (hyphen)

Tags:

syntax

oop

php

I need an escape sequence for - or the minus sign for php. The object has a name-value pair where the name happens to be having - between 2 words.

I can't do this using \ the standard escape sequence (- isn't anyways documented).

I can store the name in a $myvariable which can be used but out of curiosity is it possible to do the following?

$myobject->myweird-name

This gives an Error because of -

like image 714
umesh moghariya Avatar asked Apr 02 '12 11:04

umesh moghariya


1 Answers

This is what you need:

$myobject->{'myweird-name'};
like image 131
MichaelRushton Avatar answered Oct 18 '22 20:10

MichaelRushton