A sample text widget

Etiam pulvinar consectetur dolor sed malesuada. Ut convallis euismod dolor nec pretium. Nunc ut tristique massa.

Nam sodales mi vitae dolor ullamcorper et vulputate enim accumsan. Morbi orci magna, tincidunt vitae molestie nec, molestie at mi. Nulla nulla lorem, suscipit in posuere in, interdum non magna.

PHP 5.3.0 released

I don’t like writing posts for every new PHP release as other PHP blogs, but this time it’s worth doing it ! PHP 5.3 implements important new OOP features: namespaces late static binding and __callStatic() ! anonymous functions Phar extension (pratically, it’s the same as JAR for java files) Forother improvments and bug fixing, see [...]

array_walk example

bool array_walk ( array &$array , callback $funcname [, mixed $userdata ] )

/* example !! * transform array with other arrays as elements into an array with simple elements * array trasform ! */

$ar = array( array(1,2), array(10,20), array(100,200) );function sum(&$ar) //NOTE: Argument By reference{ $ar = $ar[0]+$ar[1];}

array_walk($ar, “sum”);print_r($ar);/* Array( [...]

PHP security: input validation and XSS

for lots of reasons, security included, it’s very important to validate all user input and protect our application from intentional or accidental wrong inputs. First of all, here are some advices don’t use register_globals ! A malicious user may change value of script variables simply modifying the query string don’t use $_REQUEST: possibile loss of [...]

XMLWriter

the PHP5 XMLWriter class allows to easily create XML well-formed documents. The most useful methods: openURI($output) // use ‘php://output‘ as argument to stream directly startDocument(’1.0′) // add the initial tag. 1st argument : xml version writeElement(‘hr’) // write the empty node [hr/] writeAttribute(‘src’,’img.gif’) //write the attribute (inside the last node) and valuee.g: [img src="http://www.blogger.com/img.gif" /] [...]

SPL: DirectoryIterator

foreach (new DirectoryIterator(‘.’) as $fileInfo) { //if($fileInfo->isDot()) continue;

echo “Name: “.$fileInfo->getFilename() . “\n”; echo “Last : “.date(“d/m/Y H:i”, $fileInfo->getCTime()) . “\n”; echo “Size: “.$fileInfo->getSize() . ” byte\n”; echo “Type: “.$fileInfo->getType() . “\n”; //dir or file echo “Executable: “.($fileInfo->isExecutable()?“yes”:“no”) . “\n”; echo “Readable: “.($fileInfo->isReadable()?“yes”:“no”) . “\n”; echo “Writable: “.($fileInfo->isWritable()?“yes”:“no”) . “\n”;}

[...]

ArrayAccess: use object as an array, file manager example

ArrayAccess (PHP5) allows to treat objects as an array. View below the interface scheme, with explanatory comments ArrayAccess { //PHP5 built-in Interface // returns true/false if the element in the position $offset exists/doesn’t existabstract public boolean offsetExists ( string $offset )

// returns the element at the position $offsetabstract public mixed offsetGet ( string [...]

References are not pointer in PHP5 !

function foo(&$var) { $var = 2; } $i = 1; foo($i); print $i; //prints “2″ but : function foo(&$var) { $i = 2; $var = &$i; print $var; //prints “2″ } $i=1; foo($i); print $i; //prints “1″ !!! Returning reference from functions: function &foo(&$var) { $i = 2; $var = &$i; return $var; } $i=1; [...]

Exceptions in PHP5

PHP5 supports Exception model, like C++/C# and Java. syntax to catch exception: try {//code which can launch an exception// [...]// manual exception throwthrow new Exception(“thrown exception”);//not executed ! (throw command is caught belowprint “not printed!”;}catch(Exception $e) { print $e->getMessage();} this code prints : “thrown exception”. It’s possibile to throw an exception inside a catch [...]

PHP5 Namespaces

Namespaces are available in PHP as of PHP 5.3.0A namespace is a container with classes, function and constants (also available in PHP>5.3.0), similar to the namespaces in C++/C# and packages in Java. As indicated in the official PHP documentation, namespaces help us to solve name collisions and use alias to call long name classes/functions. definition [...]

Late static Bindings (PHP >= 5.3.0)

in programming languages, binding is the association of the values with the identifiers. association before running: early binding association at runtime: late binding (also called virtual or dynamic) PHP5 supports the classic late binding. At runtime, PHP5 distinguishes the superclass from the subclasses object, respecting the overridden methods rules. The late binding for static methods [...]