Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8034627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:04:55+00:00 2026-06-05T02:04:55+00:00

Suppose that we have this code (simplified example): $propertyName = ‘items’; $foo = new

  • 0

Suppose that we have this code (simplified example):

$propertyName = 'items';
$foo = new \stdClass;
$foo->$propertyName = array(42);

At this point I ‘d like to write an expression that evaluates to a reference to the value inside the array.

Is it possible to do this? If so, how?

An answer that will “do the job” but is not what I ‘m looking for is this:

// Not acceptable: two statements
$temp = &$foo->$propertyName;
$temp = &$temp[0];

But why write it as two statements? Well, because this will not work:

// Will not work: PHP's fantastic grammar strikes again
// This is actually equivalent to $temp = &$foo->i
$temp = &$foo->$propertyName[0];

Of course &$foo->items[0] is another unacceptable solution because it fixes the property name.

In case anyone is wondering about the strange requirements: I ‘m doing this inside a loop where $foo itself is a reference to some node inside a graph. Any solution involving $temp would require an unset($temp) afterwards so that setting $temp on the next iteration does not completely mess up the graph; this unset requirement can be overlooked if you are not ultra careful around references, so I was wondering if there is a way to write this code such that there are less possibilities to introduce a bug.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T02:04:56+00:00Added an answer on June 5, 2026 at 2:04 am

    Ambiguous expressions like that need some help for the parser:

    $temp = &$foo->{$propertyName}[0];
    

    Btw, this is the same regardless if you’re looking for the variable alias (a.k.a. reference) or just the value. Both need it if you use the array-access notation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have this code: class Example(object): def the_example(self): itsProblem = "problem" theExample =
Suppose I have this code executed by two different threads T1 and T2 that
Suppose I have this code: static void Main(string[] args) { var thread = new
Suppose I have this function, in pseudo-code, that reverse a list: def function reverse(
I have this code that is supposed to draw two volume icons to the
Suppose that I have an integer variable in a class, and this variable may
Let's suppose that you have a website that contains a single button. When this
I have a route that is suppose to match something like this localhost:3000/site/admin and
suppose we have this next sample code: while(some condition){ lock1.lock(); . . } the
Suppose we have this code: class My_controller extends CI_Controller { private $model_name; function __construct($model_name)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.