tig12.net
Site personnel

Admin
In the section
Rubriques proches
PHP

Nombre de visites : 267
Mise en ligne : 10/2006
Dernière modif : 04/2008

 PHP wish list

Here are wishes I note while developing in php5.
Before programming in php, I was programming in java. I left java bacause it was proprietary, and too heavy to build web applications ; coding in PHP was a real pleasure, because it’s straightforward, but what a messy language, compared with the beautiful uniformity of java !

So here are features I would use if they were existing.

When I have internet at home, I’ll search on php.net wher to post that.

Classes and Objects

- Static initializers
There is no equivalent in PHP of java static initializer ; a static initializer is executed at class loading, before any object is created. The java syntax is convenient :

public myClass{
  static{
  // static initializations here
  }
}

To replace this feature, I do :

myClass::init();
myClass{
  private $initOK = false;
  public static init(){
    $initOK = true; 
  }
  public function function1(){
    if(!self::$initOK) self::init(); 
  }
  
}

- Possibility to initialize class constants
I often find myself obliged to use class variables instead of constants, to be able to initialize them. Once they are initialized, their values don’t change. So for the program, they are constants ; for the php interpreter, they are variables. I don’t know if there is a solution, if some constants could be ... variable. Maybe just give the possibility to modify them in the class’ constructor, or in a static initializer.

Particular functions

- parse_ini_file()
The possibility to parse a string, and not only a filename. Maybe a third parameter indicating if the first parameter ($filename) is a filename or a string.
Or a new function : parse_ini_string()

- php_strip_whitespace()
- Possibility to give a string, not only a filename as first parameter.
- Possibility to choose what is stripped, in particular the possibility to preserve newlines.

The PHP manual

There is a really useful page : the function index, listing all the functions. In the index page of the php manual, this link is at the bottom of page, in the "Appendices" section. It took me years before realizing that this page exists ! A better place for this link would be at the beginning of "Function Reference" paragraph.

A standard PHP code documentror

Coming from java, a standard and uniform way to document the code is badly missing in PHP.
I don’t use the existing tools (phpdocumentor, phpxref) because they impose to have useless asterisks at the beginning of comments lines (See page phpSimpleDoc for more details).
I think that a code documentor should be part of the standard distribution of php.

- Generate the php manual using this php documentor
A interesting possibility of a standard code documentor could be to use it to generate part of the PHP manual. Java is documented with javadoc, so the code produced by developers and the java APIs are in the same format. This eases the process of learning new code.

PHP syntax

- Easier syntax for class constants
In PHP, in the code of a class function, references to constants defined in an implemented interface must be typed TheInterface::CONSTANT_NAME ; in java, the syntax is directly CONSTANT_NAME.
Example :

interface TheInterface{
  const THE_CONSTANT = 3;
}
class TheClass implements TheInterface{
  public static function toto(){
    echo TheInterface::THE_CONSTANT; // php actual way
    echo THE_CONSTANT; // not valid in php - java syntax
  }
}
TheClass::toto();

This syntax is sometimes heavy : I’m storing constants in interfaces, and want to name them for ex : TimeConstants, SpaceConstants, AstronomyConstants...
Look at this code :

/** Array containing the codes of gazeous planets */
  $gazeousPlanets = array(
    SolarSystemConstants::JUPITER,
    SolarSystemConstants::SATURN,
    SolarSystemConstants::URANUS,
    SolarSystemConstants::NEPTUNE
  );

- A java-like syntax for objects

Php syntax is heavier than java’s for object manipulation :
php : $this->object
java : this.object

I personally find the "->" syntax cumbersome to type, and this leads me sometimes to stupidities (for example, design a class only with static functions, just because typing "::" is more convenient than typing "->").

Would it be possible to adapt the php interpreter so that it also undersands the java and python syntax ?
Be possible to write :

$object = new myClass();
$name = $object.getField(’name’);
instead of :
$name = $object->getField(’name’);

Maybe a patch : have a stack of syntaxes to try, recognize the current syntax, translate it to the usual php syntax, and pass it to the usual php interpreter.

A feature like that would maybe complicate life to php developers (dealing with several syntaxes), but the code would look nicer.

Miscellaneous

- Constants of type array I’ve missed several times the possibility to associate an array to a constant.

- Changing php function names and parameter order This is often pointed in forums : the lack of coherence of function names, and parameter order.
For example, each time I use substr(), I have to look at the manual.
Would it be possible to redesign the parameter order ?
As a PHP programmer, I would be ready to spend time to adapt my programs to a change like that.

To ease the transition, it could be done through a built-in class containing static methods. Each method would call the existing functions, with the redefined parameters.
ex : php2::substr(...) = substr(...)



--Site écrit avec SPIP--Licence du contenu publié sur ce site--