By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,719 Members | 1,466 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,719 IT Pros & Developers. It's quick & easy.

Problem with variable property name

P: n/a
Take the following variable:
$prefix = 'start';

And assume there is an object that has a property called 'start_time'.

How can I access that property in the object using the $prefix variable?

I tried:

$object->$prefix_time (this only yields an empty value)
and
$object->{$prefix}_year (this causes a syntax error)

Is what I'm trying to do possible?
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi Steve,
I think it is impossible but try
$var = $prefix ."_start";
$object->$var;

"Steve Dondley" <st**********@comcast.net> wrote in message
news:raocc.190600$1p.2217184@attbi_s54...
Take the following variable:
$prefix = 'start';

And assume there is an object that has a property called 'start_time'.

How can I access that property in the object using the $prefix variable?

I tried:

$object->$prefix_time (this only yields an empty value)
and
$object->{$prefix}_year (this causes a syntax error)

Is what I'm trying to do possible?

Jul 17 '05 #2

P: n/a
Steve Dondley wrote:
Take the following variable:
$prefix = 'start';

And assume there is an object that has a property called 'start_time'.

How can I access that property in the object using the $prefix
variable?

I tried:

$object->$prefix_time (this only yields an empty value)
and
$object->{$prefix}_year (this causes a syntax error)

<snip>

Haven't tried this, but I believe it would actually be:
$object->${$prefix . '_year'}

// Ian
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.