Using Entity API, if given an entity in a function scope, how do you get the entity type? This is very similar to this question: How do I get entity_type and entity_id from any entity page?
But I don't know what that asker means by "Entity Page," and in my case, using debug_backtrace (seriously?) doesn't cut it as I'd like this function to actually work from more than 1 scope.

Anyway... I am writing a module that uses numerous Entity Reference fields, and works the same code over several types of entities.  The trouble is that in my function scope I don't know the type of entity.

If I pass an Entity object into a function, I can access its type parameter:

function foo ($entity) {
  // bundle is:
  $entity-&gt;type;
}


But how do I get the "Entity Type" from this object?

For example, I have a dateset entity type with several bundles: class and retreat.  When I pass an instance of the class bundle into my function, I can identify that it's a class, and use identifier() to get its ID.  But I want my function foo() to also handle node entity types with bundles content_type_a and content_type_b

Given any instance of one of those bundles, foo() can identify the bundle:

function foo ($entity) {
  $entity-&gt;type; // class, retreat, content_type_a or content_type_b
  $entity-&gt;indentifier(); // numeric ID
}


But what about getting node vs. dateset?  Doesn't $entity know what type of entity it is?

UPDATE:
Even though I accepted Yury's answer because it provided a realistic workaround, I think the true answer is that you cannot do this in Drupal.  You have to pass around $entity_type anywhere you pass $entity (which really kind of makes the idea of having Entities as objects pretty much worthless... but I think any Drupal developer knows that object oriented concepts in Drupal are just hacked in and not ever truly related to real world object oriented design).  See this Drupal issue: http://drupal.org/node/1042822
There are various (convoluted) methods for getting the entity type and bundle, but none that I'd consider "acceptable" on a large-scale. What I've done is implemented hook_entity_load() and added the entity_id and entity_type properties on all entities at load-time. 

The following code does this, and is part of the Devtools module I maintain:

function hook_entity_load($entities, $type) {
  // add the entity type &amp; id to all entity objects for easy data access
  // from other hooks &amp; modules
  foreach ($entities as $entity) {
    // we don't want to add these entity fields when the entity is being exported into
    // a feature, otherwise the feature will always show as overridden and will not 
    // revert
    if (  preg_match('#admin/structure/features#', $_GET['q'])  // features page
            || 
          (                                                     // drush fu command
            isset($_SERVER['SCRIPT_FILENAME']) &amp;&amp; 
            preg_match('/drush\.php$/', $_SERVER['SCRIPT_FILENAME']) &amp;&amp;
            isset($_SERVER['argv'][2]) &amp;&amp;
            $_SERVER['argv'][2] == 'fu'
          )
       ) {
      continue;
    }

    // set entity_type
      $entity_type_field = (!isset($entity-&gt;entity_type)) ? 'entity_type' : 'devtools_entity_type';
      $entity-&gt;{$entity_type_field} = $type;

    // set entity_id
      $entity_id_field = (!isset($entity-&gt;entity_id)) ? 'entity_id' : 'devtools_entity_id';
      list($entity-&gt;{$entity_id_field}, $vid, $bundle) = entity_extract_ids($type, $entity);
  }
}