Exact difference between Component an Behavior

Hi,

Can any one explain the Exact difference between Component and Behavior please.

Thanks.

A component allows a class to subscribe/consume events and it also uses magic getters/setters.

Events (only pseudo code and not tested):




class UploadComponent extends CComponent

{

    public $image;


    public function onUploadComplete($event)

    {

        $this->raiseEvent('onUploadComplete', $event);

    }


    public function upload()

    {

        // do your upload stuff here.

        $this->image = '/var/www/uploads/yo_mamma.jpg';

        $this->onUploadComplete(new CEvent($this));

    }

}


class ResizeComponent extends CComponent

{

    public function __construct()

    {

    }


    public function handleUploadComplete(CEvent $event)

    {

        // You should verify $event's type before accessing properties.

        if ( $event instanceof (UploadComponent ))

        {

            $this->resize($event->sender->image);

        }

    }


    private function resize($image)

    {

        // Do your stuff here.

    }

}


class UploadManager

{

    private $uploadComponent;

   

    public function __construct()

    {

        $this->uploadComponent = new UploadComponent();

        $resizeComponent = new ResizeComponent();


        // Subscribe to UploadComponent's event

        $this->uploadComponent->onUploadComplete = array($resizeComponent, 'handleUploadComplete');

    }


    public function beginUpload()

    {

        $this->uploadComponent->upload();

    }

}


// Usually in a controller

$uploadManager = new UploadManager();

$uploadManager->beginUpload();



Getters/Setters:

These are a convenience, especially when coming from a language like Java or C#. You write your get/set in the traditional manner:




// getter, defines a readable property 'text'

public function getText() { ... }

// setter, defines a writable property 'text' with $value to be set to the property

public function setText($value) { ... }



But it allows you to access/set them the old skool way:




$a=$component->text;     // equivalent to $a=$component->getText();

$component->text='abc';  // equivalent to $component->setText('abc');



Behaviors allow you to extend a class (more methods) without physically adding the methods. This allows you to write the behavior once and reuse it wherever you like.

See this article for a good summary.




class Output extends CBehavior

{

    public function toString(array $properties)

    {

        $output = '';


        foreach ($properties as $property)

        {

            $output =. $property . '<br />';        

        }


        return $output;

    }

}


class User extends CActiveRecord

{

    public behaviors()

    {

        return array(

            'Output' => array(

                'class' => 'path.to.Output',

            ),

        );

    }

}


$user = $this->loadUser(123);

echo $user->toString();



Hope that makes sense.

Matt

Thanks a lot,Matt.

If you aware with cakePHP It provides in following way.

  1. Component to extend Controller

  2. Behaviour to extend Model

  3. Helper to extend View

And the similar type of thing are in Yii also extra is widget instead of helper.

Please refer http://www.yiiframework.com/doc/guide/1.1/en/extension.create for more info.