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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:13:42+00:00 2026-06-11T08:13:42+00:00

I have the following string: $str = methodA()->methodB()->methodC And i want to call that

  • 0

I have the following string:

$str = "methodA()->methodB()->methodC"

And i want to call that “chain” on an object

$obj->$str

I am currently splitting with ()-> and calling one by one, but there must be abetter way.

How can i call it on one line? thanks!

EDIT: Some more context information:

I created a Doctrine Behaviour for symfony 1.4 to be able to make certain tables from the schema searchable with Zend Lucene.

In short, i need a way to say to the behaviour “To get the searchable field you must call methodA()->methodB()->methodC()”

EDIT2:
Maybe i wasnt clear enough. $obj and $str are determined at runtime, so some of your suggestions are not applicable.

EDIT3:
In case you are wondering, i am currently doing this:

  <?php
  $chain = explode("()->",$str);
  $method = array_shift($chain);
  $value = $obj->$method();
  foreach($chain as $method){
    $value = $value->$method();
  }
  • 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-11T08:13:44+00:00Added an answer on June 11, 2026 at 8:13 am

    After writing my comment to the main question I thought of a possible solution, though a little verbose and manual (you would have to make as many case statements as you would reasonably expect for the number of chained methods).

    function chain_execute($obj, $str) {
        $chain = explode("->",$str);
        $chain = array_map('trim', $chain);
        $chain_count = count($chain);
        switch($chain_count) {
            case 1:
                return $obj->{$chain[0]}();
                break;
            case 2:
                return $obj->{$chain[0]}()->{$chain[0]}();
                break;
            case 3:
                return $obj->{$chain[0]}()->{$chain[1]}()->{$chain[2]}();
                break;
            // etc.
            default:
                // not a value you were expecting
                throw new Exception('Invalid number of methods chained: ' . $chain_count);
                break;
        }
    }
    

    Usage would be:

    $result = chain_execute($obj, "methodA->methodB->methodC");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Supposed I have the following string: string str = <tag>text</tag>; And I would like
I have the following string: const char *str = \This is just some random
I have a string like the following: String str = 4*5; Now I have
Suppose I have the following code: foreach(string str in someObj.GetMyStrings()) { // do some
I have the following function: public static byte[] StringToByte(string str) { int length =
I have the following code: int sec = 62; string str = string.Format(Time: {0:xxx},
I have the following situation: function Mach3Code(Str: String): String; var StrOut: String; begin StrOut
If I have for example the following string: 123;3344;4334;12 and I want these numbers
I have the following code to parse a String variable called str. NumberFormat formatter
Given I have code like the following: void foo() { String str = hello;

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.