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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:31:40+00:00 2026-06-07T20:31:40+00:00

This is an interesting quirk I found while method chaining and I’m having difficulty

  • 0

This is an interesting quirk I found while method chaining and I’m having difficulty bypassing it. I’m sure there’s a solution or another way. It’s tough to explain but I’ll do my best.

Example:

You have three functions which are part of a class, as well as 2 protected properties as follows.

class Chain {

  protected $_str  = '';
  protected $_part = 0;

  public function __toString() {
    return implode(' ', $this->_str);
  }

  public function AAA () {
    $this->_str[$this->_part] = 'AAA';
    $this->_part++;
    return $this;
  }

  public function BBB () {
    $this->_str[$this->_part] = 'BBB';
    $this->_part++;
    return $this;
  }

  public function wrap ($str) {
    $part = $this->_part - 1;
    $this->_str[$part] = "({$str})";
    return $this;
  }

}

Now when chaining these methods and specifically using the wrap method, the strings from previous chains are unintentionally appended. Example:

$chain = new Chain();
$chain->AAA()->BBB()->wrap($chain->AAA());
echo $chain;

What you would expect the string to look like is AAA BBB (AAA).

However, what actually returns is AAA BBB (AAA BBB AAA).

Why is it that wrap() takes all the previous methods called within the chain instead of only the method that’s actually wrapped by it? What is the best way around this assuming there is one?

  • 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-07T20:31:41+00:00Added an answer on June 7, 2026 at 8:31 pm

    $chain->AAA()->BBB() is doing the first two ‘AAA’ and ‘BBB’ – obvious.
    Then the $chain->AAA() which comes inside wrap($chain->AAA()) does the 3rd ‘AAA’.
    and last, the wrap method takes all the three and wraps them with () and concatenated to the first ‘AAA’ and ‘BBB’ using this line: $this->_str[$part] = "({$str})";
    which resolves to: AAA BBB (AAA BBB AAA).

    UPDATE:
    I believe that what you’re trying to do, is to avoid the side-effect of returning this from methods AAA() and BBB() – will be achieved with the following changes:

    <?php
    class Chain {
    
      protected $_str  = '';
      protected $_part = 0;
    
      public function __toString() {
        return implode(' ', $this->_str);
      }
    
      public function AAA () {
        $this->_str[$this->_part] = 'AAA';
        $this->_part++;
        return "AAA";
      }
    
      public function BBB () {
        $this->_str[$this->_part] = 'BBB';
        $this->_part++;
        return "BBB";
      }
    
      public function wrap ($str) {
        $part = $this->_part - 1;
        $this->_str[$part] = "({$str})";
        return $str;
      }
    
    }
    
    $chain = new Chain();
    $chain->AAA();
    $chain->BBB();
    $chain->wrap($chain->AAA());
    echo $chain->__toString();
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found this interesting behavior. Why could this happen I am using the cmdlet
there's this interesting problem i can not solve myself. I will be very glad,
I found this interesting site, that helps me understand derivatives, http://www.numberempire.com/derivatives.php , It seems
There is this interesting game, that has numbers in a grid, where each number
In Matters Computational I found this interesting linear search implementation (it's actually my Java
I found this interesting interface(starts at 33 seconds http://vimeo.com/22946428 ), and would like to
I've always found this interesting, and haven't managed to fully understand it yet. Take
I noticed this interesting use of the this keyword while viewing the disassembled code
I was reading this interesting post on metaclasses What is a metaclass in Python?
I have this interesting obstacle on my project. I am currently doing a MVC3

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.