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 6173733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:37:38+00:00 2026-05-23T23:37:38+00:00

I’m attempting to set up a variable-based object operator in PHP, but am only

  • 0

I’m attempting to set up a variable-based object operator in PHP, but am only able to accomplish what I am looking for to a limited extent. For example, the following code allows for variable selection:

$var1 = 'available_from';
$keyValuePairs[$key] = $item->parent()->{$var1};

However, if I want to make the parent selector a variable as well, I no longer seem to be able to. Both of the following methods fail:

$var1 = 'parent()->available_from';
$keyValuePairs[$key] = $item->{$var1};

and

$var1 = 'parent()';
$var2 = 'available_from';
$keyValuePairs[$key] = $item->{$var1}->{$var2};

So the question is whether there is a way to do this.

  • 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-05-23T23:37:38+00:00Added an answer on May 23, 2026 at 11:37 pm

    You can basically do that, but you have to put the parens on the outside.

    $var1 = 'parent';
    $var2 = 'available_from';
    $keyValuePairs[$key] = $item->{$var1}()->{$var2};
    // or $keyValuePairs[$key] = $item->$var1()->{$var2};
    

    And there basically is no way of getting around that without using eval:

    // escape the first $
    $keyValuePairs[$key] = eval( "\$item->$var1->$var2" );
    

    But, there is really no reason to use eval if you have access to the potential set of variables first.

    You can do something like this to get around it:

    function call_or_return( $obj, $prop )
    {
        // test to see if it is a method (you'll need to remove the parens first)
        $arr = array( $obj, $prop );
        // if so call it.
        if( is_callable( $arr ) ) return call_user_func( $arr );
        // otherwise return it as a property
        return $obj->$prop;
    }
    
    call_or_return( $item, $var1 )->{$var2};
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: C# generic constraint for only integers Greets! I'm attempting to set up
A make script is attempting to set a variable as follows: VER_DEC=$( perl -e
I am attempting to set an asp.net textbox to a SQL 2005 money data
I'm attempting to set up a symbol proxy using symproxy.dll from Debugging Tools for
I am attempting to set a value in a textarea field using jquery with
I'm attempting to set up basic logging to the windows event log in .net
I am attempting to set up my own project using a third party library
I have a Perl script that I'm attempting to set up using Perl Threads
I am attempting to run the following command in KornShell (ksh): set -A INDEXES
I am attempting to use the code-behind (Page_Load or PreRender) to set a date-time

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.