Sunday, December 16, 2007

Reflection Class

Since PHP 5 is a set of classes and interfaces that allow us to obtain information about the classes, functions, methods, attributes and objects. This package allows us, for example, to know how many parameters receives a function, and how many are required ... One example of how to use this

/**
* This function repeats
*
* @param string $text
* @param integer $times
*/
function repeat($text, $times = 2) {
return str_repeat($text, $times);
}
$reflection = new ReflectionFunction('repeat');
echo 'User defined: ' , $reflection->isUserDefined() , "\r\n";
echo 'Documentation: ' , $reflection->getDocComment() , "\r\n";
echo '# Params: ' , $reflection->getNumberOfParameters() , "\r\n";
foreach ($reflection->getParameters() as $parametro)
{
echo 'Parameter "' , $parametro->getName() , '" ' , ($parametro->isOptional() ? '' : 'no ') , 'is optional' , "\n";
echo 'By default: ' , ($parametro->isDefaultValueAvailable() ? $parametro->getDefaultValue() : 'doesn\' have any') , "\n";
}
?>

Sunday, December 9, 2007

JSON

JSON is a format for the transmission of data useful to move from one code to another.

The most common example is to move an object or an array from PHP to JavaScript and vice versa. For this, PHP 5 provides two features that make the transfer: json_encode and json_decode. There are classes that offer a similar operation for PHP 4.

The advantage of this notation is transparency and ease with which data is transmitted. XML is an alternative which also presents its advantages. It is preferable to a series of data, if they want to consult with XQuery, but for the transmission of single data, JavaScript syntax is presented simpler.