Archive for the 'Language' Category

October 31st 2006

Prototype Javascript Framework

If you do a lot of Javascript development, and haven’t heard of Prototype, this post will (hopefully) change your life forever. Prototype is a Javascript framework written by Sam Stephenson to create a common foundation for Javascript development.

Download the library from here:
http://prototype.conio.net/

Read the documentation here:
http://www.sergiopereira.com/articles/prototype.js.html

and learn how to use it from the very nicely done quick start guide here:
http://particletree.com/features/quick-guide-to-prototype/

No Comments yet »

October 24th 2006

Print_r to a String

Every PHP developer knows about the print_r function, but for some reason, not many of them know that you can capture the output of print_r and avoid it being printed directly to stdout (the browser in most cases). The print_r function has a little know second argument, true or false, which tells it what to do with the output. By default this is false, which means “don’t return, print to stdout”, if you set this to true, print_r will return a string, which you can do whatever you want with.

<?php
  $o = new stdclass();
  $o->name = "brien";
  $o->website = "http://code-a-day.com/";
  $o->date = "today";
  print_r($o); // prints to stdout (the browser window)
  // a common thing i do is print to the error log:
  error_log( print_r($o, true) ); // prints to the default apache error log
  // save it in a variable
  $data = print_r($o, true);
?>

No Comments yet »

October 23rd 2006

Douglas Crockford’s “Advanced Javascript” Talk - Now Public!

Think you know Javascript? Chances are you don’t realize how powerful this language really is. Every few months at Yahoo! Douglas Crockford, our JavaScript Architect, gives an awesome talk that is now available to the public via Yahoo! Video


Advanced Javascript Part I


Advanced Javascript Part II


Advanced Javascript Part III

No Comments yet »

October 22nd 2006

PHP5 and the SPL - Pt1: ArrayObjects

With the Standard PHP Library (SPL) available in PHP5 a new object is available to make arrays a little more usefull. Java developers wanting to learn PHP will find comfort in PHP’s ArrayObject.

to create an array object, just new it:

<?php
  $arrayObj = new ArrayObject();
  // or create one from an existing array:
  $normal_array = array();
  $normal_array[]  = "item one";
  $normal_array[] = "item two";
  $normal_array[] = "item three";
  $arrayObj = new ArrayObject($normal_array);
?>

to add and access data, it’s super easy:

<?php
  $arrayObj->append("item four");
  $arrayObj->offsetGet(3); // get the value at offset 3
?>

my favorite part about ArrayObjects is the ability to get an ArrayIterator for iterating over the values

<?php
  $iter = $arrayObj->getIterator();
  // i now have my iterator that i can iterate through in a couple different ways
  // 1) Java-esq
  while($iter->valid()){
    $curVal = $iter->current();
    // do something with the value
    // iterate to the next value
    $iter->next();
  }
  // 2) as an array in foreach
  foreach((array)$iter as $val){
    // do something with $val
  ...
  }
?>

This is just a quick intro to the ArrayObject, to see all of the features, see the SPL docs, or the in depth SPL doxygen pages

No Comments yet »

October 21st 2006

Switching Between PHP Objects and Arrays

Switching between Array and Object data structures is a snap with PHP’s object casting and the get_object_vars() function.

To switch from an Array to an Object, it’s a matter of a simple typecast

<?php
// start with an array of data
$personArray = array();
$personArray['name'] = "Brien";
$personArray['age'] = 29;
$personArray['gender'] = "male";
// convert to an object
$personObject = (object)$personArray;
// now all keys of $personArray are member variables of $personObject
// the data can now be accessed as member variables:
print( $personObject->age); // prints 29
?>

Converting the other way is just as easy using PHP’s get_object_vars() function

<?php
// start with a simple stdclass data structure
$personObject = new stdclass();
$personObject->name = "Brien";
$personObject->age = 29;
$personObject->gender = "male";
// convert to an Array of data
$personArray = get_object_vars($personObject);
// now all members of $personObject are keys in $personArray
// the data can now be accessed through Array indexes:
print( $personArray['age']); // prints 29
?>

No Comments yet »

« Prev