DAGLI ESPERTI DI COMTASTE

Immagini

Immagine 1

Better PHP code: some improvements, resources and tips

In this post I want to show some resource and tips to improve, validate and document your PHP code, and also if you want how to be compatible when PHP 6 when will be available.

PHPLint - http://www.icosaedro.it/phplint/index.html
A validator and a documentator of PHP programs. The programmer can add to the source the PHPLint meta-code, that enhance the syntax of the PHP language toward the paradigm of a strong-typed language and safe programming.

Enablig the Enterprise with Zend - http://files.zend.com/videos/enterprise_php/
Zend Platform is the only application server for PHP that supports the performance, management, integration, and enterprise scalability requirements of organizations using PHP for their business-critical applications. By providing capabilities that streamline development and deployment, Zend Platform improves the user experience, application responsiveness, integration to your existing infrastructure, and increases application reliability and scalability.

Java technologies:
PHP has an extension to interact with Java.
This extension gives access to all Java APIs (calls are only by values).
Another bridge is hosted at SF.net
Java APIs related to distributed programming are RMI and JMS.
The protocol is binary thus giving performance advantage.

RMI - architecture:
Things to know when working with Java
Classes are instantiated with Java class: $string = new Java(‘java.lang.String’);
When the class has no public constructor the PHP variable is an object of class java.lang.Class.
Calling static method is no different than calling normal methods.
Java exceptions are PHP errors. A E_WARNING will be generated on exception.
java_last_exception_get() tell you if there was an exception. Use java_last_exception_clear() to clear it.
One can define his own error handler.

Accessing remote service over RMI:

<?php
function gettime() {
return array_sum(explode(‘ ‘, microtime()));
}

function err_handler($errno, $errstr, $errfile, $errline) {
if ($ex = java_last_exception_get()) echo $ex->toString().”\n”;
java_last_exception_clear();
}

set_error_handler(“err_handler”);

$class = new Java(“java.rmi.Naming”);
$calculator = $class->lookup(“rmi://localhost/CalculatorService”);
var_dump($calculator->sub(14,3));
$i = 0;
$start = gettime();
while ($i++ < 500) $calculator-> sub($i, 20);
$end = gettime();
printf(“Finished %d calls in %2.5f\n”, $i1, $end-$start);