Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Yii behavior that not only checks the normal object inheritance, but also attached behaviors.
PHP
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
.gitignore
InstanceofBehavior.php
README.md

README.md

This behavior allows you to quickly check if a given object, or behaviors that might be attached to the object, implement an interface or inherit from some class. It is essentially the same as PHP's instanceof operator, but takes Yii's behaviors into accout.

Requirements

  • PHP >= 5.3
  • Yii >= 1.0.2

Usage

  1. Extract the behavior
  2. Attach the behavior to your application:

    
    // application.config.main
    return array(
    
      'behaviors' => array(
        'instanceof' => array(
          // alias to the behavior
          'class' => 'ext.behaviors.InstanceofBehavior',
        ),
      ),
    
      // more config stuff...
    );
    
  3. Use it like this

    
    // quick version
    // check if $model or one of the behaviors attached to $model implement IRestResource
    if (Yii::app()->isInstanceof($model,'IRestResource'))
    {
      /* @var $model IRestResource */
      // work on $model as if it was an IRestResource
    }
    
    // safe version
    // check if $model or one of the behaviors attached to $model implement IRestResource
    // and return the object that implements it (this might be $model itself, or one of the
    // behavior instances attached to $model). This way, even if $model and the returned
    // behaviors have methods with the same name, you will call the correct method (the
    // method of the object implementing the interface).
    if ($instance=Yii::app()->getInstanceof($model,'IRestResource'))
    {
      /* @var $instance IRestResource */
      // work on $instance. It is an IRestResource.
    }
    
Something went wrong with that request. Please try again.