Tobiasz Cudnik

Archive for the ‘The Net’ Category

PHP 5.3 introduces closures and inline functions

In The Net on 25.11.2008 at 0:31

I couldn’t believe it in first place, but there are ongoing works to bring closures power to PHP in upcoming 5.3 version! No more create_function() and backslashing backslashed quotes.

There are also some scope inheritance tries (using “use” keyword). Below you have some teasing snippet, after which head to wiki.php.net/rfc/closures for more…

function replace_in_array ($search, $replacement, $array) {
  $map = function ($text) use ($search, $replacement) {
    if (strpos ($text, $search) > 50) {
      return str_replace ($search, $replacement, $text);
    } else {
      return $text;
    }
  };
  return array_map ($map, $array);
}

jQuery Server Side ports

In The Net on 01.11.2008 at 11:03

jQuery besides achieving such successes as being used by Google or Micro$soft also has ports to other major languages. Most of them are designed to be server-side what opens doors for new uses to the library.

jQuery ports to other languages:

Below some snippets showing each implementation in few lines.

PHP

foreach($doc['ul > li'] as $li) {
  pq($li)->addClass('my-new-class');
      ->filter(':last')
        ->addClass('last-li');
}
$doc['ul > li:last']
  ->addClass('last-li');

Ruby

# load the RedHanded home page
doc = Hpricot(open("http://redhanded.hobix.com/index.html"))
# change the CSS class on links
(doc/"span.entryPermalink").set("class", "newLinks")

Perl

pQuery("http://google.com/search?q=pquery")
  ->find("h2")
    ->each(sub {
      my $i = shift;
        print $i + 1, ") ", pQuery($_)->text, "n";
      });

ActionScript

// add enterFrame event handler
$(stage).enterFrame(function(event:Event):void {
  $("RoundRect").attr("color", function(...args):uint {
    return Math.random() * 0xffffff;
  });
});

List comes from phpQuery wiki page. Do you know any other ports ? Share it in comments.

Update 09.12.08
New jQuery port has been released. This time it’s powered by Python and named PyQuery. Here’s the code:

Python

p = d("#hello")
p.addClass("toto")
p.attr.id = "plop"
p.prependTo(d('#test'))

Rails’ ActiveSupport ported to PHP

In The Net on 05.10.2008 at 17:16

It’s year old but found it today on benlog.org. I’m verry happy since i’ve been working on project providing similar chains and now i can use well-thought Rails equivalent. Yet i will still write my own implementation as it differs in many ways.

require_once('ActiveSupport.php');
// Outputs "14th"
_(14)->ordinalize();
// Returns number of bytes in 7.3 megabytes
_(7.3)->megabytes();
// Returns true
_("an example sentence")->endsWith("sentence");

I really liked the idea of ‘_‘ suffix to change returned type. See lots of great examples in Rails Rubyisms Advent.

Follow

Get every new post delivered to your Inbox.