Yii Framework Forum: a using() statement in php - Yii Framework Forum

Jump to content

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

a using() statement in php more php sugar and/or abuse :-) Rate Topic: -----

#1 User is offline   mindplay 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 397
  • Joined: 03-September 09
  • Location:New York

Posted 05 August 2010 - 07:42 PM

Since forever, I miss my using() block statement, which I had in Pascal/Delphi, and lately have been addicted to anew while working with C# ;-)

Here's what I came up with:


<?php

class UsingStack
{
  private $_objects=array();
  
  public static function instance()
  {
    static $instance;
    if (!isset($instance))
      $instance = new self;
    return $instance;
  }
  
  public function using($object)
  {
    if (is_object($object))
      $this->_objects[] = $object;
    else
      $this->_objects[] = $this->current($object);
  }
  
  public function done($object=null)
  {
    if (count($this->_objects)==0)
      throw new Exception(__CLASS__."::end() without matching begin()");
    if ($object!==null)
      if (array_pop($this->_objects)!==$object)
        throw new Exception(__CLASS__."::end() with mismatching begin()");
    array_pop($this->_objects);
  }
  
  public function current($name=null)
  {
    if ($name===null)
      return end($this->_objects);
    else
      return end($this->_objects)->$name;
  }
}

// a class-less API for convenience:

function using($object)
{
  UsingStack::instance()->using($object);
}

function done($object=null)
{
  UsingStack::instance()->done($object);
}

function this($name=null)
{
  return UsingStack::instance()->current($name);
}

// sample object:

$test = (object) array(
  'name' => 'Rasmus',
  'age' => '35',
  'projects' => (object) array(
    'work'=>'Building websites',
    'home'=>'Fencing the lawn',
  ),
);

?>
<html>
  <head></head>
  <body>
    <pre><? var_dump($test); ?></pre>
    <table>
      <? using($test) ?>
        <tr><td>Name:</td><td><?=this('name')?></td></tr>
        <? using('projects') ?>
          <tr><td>Work Project:</td><td><?=this()->work?></td></tr>
          <tr><td>Home Project:</td><td><?=this()->home?></td></tr>
        <? done(); ?>
        <tr><td>Age:</td><td><?=this()->age?></td></tr>
      <? done($test) ?>
    </table>
  </body>
</html>


So the "block" consists of a pair of using($object)..done() statements, and in between those, you can reference the current object with this(), or a property of the current object with this($name).

It's not quite the same as a real using statement, but you do get the benefit of being able to refer to the current object in a uniform manner, and the ability to walk back up the stack, restoring the reference to the previous object.

Sort of pointless, maybe - but pointless fun! ;-)
0

#2 User is offline   DarkNSF 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 287
  • Joined: 12-November 08
  • Location:Palm Bay, Florida

Posted 06 August 2010 - 05:16 PM

pretty interesting, i like it!

buuuuuuuuutwhyshorttagss :-X
0

#3 User is offline   Sniper 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 55
  • Joined: 03-March 10
  • Location:Planet Ruby on Rails

Posted 07 August 2010 - 05:06 AM

I'd consider it pointless fun :)
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