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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:38:23+00:00 2026-06-05T05:38:23+00:00

At this point, let’s say I have a class like so: class Sentence {

  • 0

At this point, let’s say I have a class like so:

class Sentence {
  __construct($content) {
    $this->content = $content;
  }
  public function grab_word($num) {
    $words = explode(" ", $this->content);
    return $words[$num+1];
  }
}

So the interface given would allow me to create a new Sentence, and then I could call the grab_word() class method to fetch a word in the sentence:

$sentence = new Sentence("Lorem ipsum dolor sit amet");
echo $sentence->grab_word(2);

However, what I’d like to do is add another chained class method, giving me the ability to capitalize this word.

$sentence->grab_word(2); # -> ipsum
$sentence->grab_word(2)->caps(); # -> IPSUM

Clearly, this won’t work because chained methods require object inheritance. If I were to create caps() and chain that function – it would return an error due to the return not being the inherited Sentence object.

To sum it up, my question is how to I achieve the ability to chain these methods optionally, but still return a non-object whenever needed (like in my output example).

  • 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-05T05:38:26+00:00Added an answer on June 5, 2026 at 5:38 am

    Broadly speaking, what you’re asking is not possible. Why don’t you just always return the object you sometimes need to chain method calls onto? Just return new Word($words[ ... ]) and you’ll be in a better position.

    Now, when I say “broadly speaking”, there are things you can do to make it possible to call different methods, that may live in different classes. You can make use of __call() to implement something similar to ruby’s mixins, though really quite inferior. __call() will simply look at the method name that was invoked, then iterate a list of possible targets and see if they respond to that method, before delegating the invocation to that object (or raising an exception).

    PS: This question hasn’t got anything to do with inheritance. Method chaining requires returning an object, not just implementing an inheritance hierarchy. Of course, you may return $this, where that makes sense, but usually you return another object.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Let's say I have data string like this... one=1&two=2&three=3&four=4&two=2 I'm using php foreach to
Let's say that I have a class/table called Images that, as it stands right
Let's say I have an ActiveRecord called Apples, and I want a class method
Let's say I have the following class X where I want to return access
Let's say that I have a perspective view in OpenGL and choose a point
This is a completely hypothetical question: let's say I have a database where I
Up to this point we have not yet needed a new masterpage for our
I have create a module which at this point does nothing but exist the
I am faced with the following issue and at this point I feel like

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.