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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:36:08+00:00 2026-05-26T08:36:08+00:00

Can I write chainable functions in CodeIgniter? So if I have functions like these

  • 0

Can I write chainable functions in CodeIgniter?

So if I have functions like these :

function generate_error(){
    return $data['result']      =   array('code'=> '0',
                                      'message'=> 'error brother');
}

function display_error(){
      $a= '<pre>';
      $a.= print_r($data);
      $a.= '</pre>';
      return $a;
}

I want to call those by chaining them :

echo $this->generate_error()->display_error();

The reason why I want to seperate these functions are because display_error() is only useful for development, so when it comes to production, I can just remove the display_error() or something like that.

Thanks!

  • 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-26T08:36:08+00:00Added an answer on May 26, 2026 at 8:36 am

    To write chainable functions they musy be part of a class, from the function you then return a reference to the current class (usually $this).
    If you return anything other than a reference to a class it will fail.

    It is also possible to return a reference to another class (e.g. when you use the code igniter active records class get() function it returns a reference to the DBresult class)

    class example {
    
      private $first = 0;
      private $second = 0;
    
      public function first($first = null){
        $this->first = $first;
        return $this;
      }
    
      public function second($second = null){
        $this->second = $second;
        return $this;
      }
    
      public function add(){
        return $this->first + $this->second;
      }
    }
    
    $example = new example();
    
    //echo's 15
    echo $example->first(5)->second(10)->add();
    
    //will FAIL
    echo $example->first(5)->add()->second(10);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can write a function like : public function removeRubbishTag(&$aString) Also, I can have
So I know I can write my own HTML-encoding function like this: function getHTMLEncode(t)
When I write code in Python with exception handling I can write code like:
i can write methods like that public void CompareEmail() { some code } public
I can write the code like this: str match { case s if s.startsWith(!!!)
I can write and add data on C# ( form application ) then I
I can write my code logic to either buffer a polyline or to buffer
In SQL I can write SELECT blah FROM Clients Where @p1 Like '%'+lastname+'%' How
As we can write in SQL to return single static value select Jan as
how i can write a closed function for our wpf application in Window application

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.