Jump to content

Recommended Posts

I've seen some people code with objects in PHP, but one thing that others do (that I'd like to do) is the ability to call multiple methods at once. For example, in Zend Framework it's possible to write the following:

 

$variable->setMessage('Text')->addColor('blue')->toArray(); (just an example)

 

Notice how you can call several methods at once without having to write $variable next to each method? I've tried to do similar stuff in my code but cannot achieve this effect. Does anyone know how to set this up or point me in the right direction? Thanks!

Link to comment
https://forums.phpfreaks.com/topic/208180-php-objects-and-methods-question/
Share on other sites

Hi,

 

Its a great question  ;D and exciting at all.

 

Heres a simple example what im just done:

 

class class_a extends class_b
{
    public function func_a()
    {
        echo 'm';

        return $this;
    }
}

class class_b
{
    public function func_b()
    {
        echo 'n';
    }
}

$a = new class_a();

$a->func_a()->func_b();

 

Hi,

 

Its a great question  ;D and exciting at all.

 

Heres a simple example what im just done:

 

class class_a extends class_b
{
    public function func_a()
    {
        echo 'm';

        return $this;
    }
}

class class_b
{
    public function func_b()
    {
        echo 'n';
    }
}

$a = new class_a();

$a->func_a()->func_b();

 

Excellent! So all subsequent methods must be extensions of the last method called, makes a lot of sense when trying to build easy to read, flexible code. Thanks for your help!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.