Yii Framework Forum: Exact difference between Component an Behavior - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Exact difference between Component an Behavior Rate Topic: -----

#1 User is offline   Bhan 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 11-March 11

Posted 17 August 2011 - 12:35 AM

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

Thanks.
0

#2 User is offline   waterloomatt 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 545
  • Joined: 09-April 10

Posted 17 August 2011 - 03:03 AM

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
0

#3 User is offline   Bhan 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 11-March 11

Posted 17 August 2011 - 06:45 AM

View Postwaterloomatt, on 17 August 2011 - 03:03 AM, said:

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.
0

#4 User is offline   anup 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 18-January 12
  • Location:INDIA

Posted 31 October 2012 - 02:16 AM

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.yiiframew...xtension.create for more info.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users