(English) An alternative of Hungarian notation applied to PHP

On some cases, by example when you works on webservices, data import or an ETL, you can be a little lost between all the variables you use, specifically arrays.

Even if it’s not in the coding standard, I developed a derivative of the Hungarian notation to prefix my variables and find myself there. This notation can be applied in php, but also to any other languages, especially non-typed.

NOTE : I recommend NOT using hungarian notation everytime (for exemple, will be almost useless in a MVC context), but only when it’s really needed.

There is the prefixing I use with PHP variables in that specific cases, depending on their type and semantic meaning :

Scalar types prefix

  • $is, $b : boolean
  • $s : string
  • $i : int (signed)
  • $n : natural (unsigned int)
  • $f : float
  • $fd : float round to 1 decimals
  • $fc : float round to 2 decimals
  • $fm : float round to 3 decimals
  • $m : mixed.
  • $v : void / null.

Non-scalar prefix types

  • $o : object
  • $r :
  • $h : handler (type in PHP : resource).

Arrays, can be, often, by design, a collection of elements, or a single element. For me an “element” is an object-like data, stored as an associative array. While a collection if an array of elements.
So I can distinguish 3 different semantic types for arrays :

  • $c : Collection [auto-incremented keys] : an array with integer keys containing elements (like « list » in Python). The keys are not used neither assigned, and only usefull to know the processing order.
  • $a : Dictionnary [associative keys] : the array keys are important and defined for each index (like in dictionnary Python or hash in Perl).
  • $e : Element [associative keys] ; like $a, but with a semantic information : contains data about one element. Think about an Object converted in Array.
    important.

For Collection and Dictionnary variables, I usually also put the variable in plural (or simply suffix with an s). But for Element, I put the variable name singular

For some array, i can also combine prefixes, like :

  • $co : incremented-collection of objects
  • $ce : incremented-collection, and each index contains an associative array defining an Element
  • $as : a dictionnary of string elements
  • $cbo : increment-collection of boolean OR objects. So we have to test if the entry is an object before processing it.

Other semantic types :

Date :
$ts : timestamp (unsigned int)
$dt : datetime, format ‘Y-m-d H:i:s’ (string)
Others :
$h : string of html code
$j : string of json code
$x : string of XML code
$y : string of YaML code
$u- : unsafe type or value, need to be checked or sanitized for security reason.
$z- : string of encoded something
They can be combined :
$us for unsafe string, with possible code injection.
$zo : string of encoded object
$zj : string of encoded json
$zh : string of encoded html entities, ie: ‘"’

Prefixes for Classes functions :

set : setter, assign the parameter function value to a class member
get : getter, returns something (can be the value of a class member)
is : returns a boolean

I personnally never prefix private members/function with underscores (indicates the visibilty is not public, usually), or with the prefixes I used below for variables.
Because i usually put all members in protected, and almost all function in public : some can be protected, but it can also change with time. So I avoid borgin refactoring when I change the visibilty of something.

Un commentaire ?