What is Php ?

Stands for Php : Hypertext Preprocessor. Heterogeneous server language, Php is uncommon by its history. Created originally as a script language with a dynamic type hinting and generating Html code, Php is now able to offer an advanced object paradigm and its applications, although historically related to the websites creation, exceed widely these prejudices and provide tools to quickly design reliable and efficient softwares (Remember that Php is closely linked to the C language).

The dynamic type hinting of variables

The power of Php is fundamentally based on the dynamic type hinting of variables. By this way, it is possible to use a single variable in order to store multiple types of content and build dynamically variable names.

$a = 21;   // $a is an integer.
$a = '21'// $a is now a string (dynamic type hinting).
 
$a = 'b';
$b = 42; 
echo $$a;  // Display 42 ($$a --> ${$a} --> ${'b'} --> $b --> 42).
 
$b = 'myFunction';
function myFunction() {
  return 63;
}
echo $b();   // Display 63 ($b() --> ${'myFunction'}() --> myFunction() --> 63).

The associative arrays

The arrays, as used by Php, provide a great ease of handling. Associative by nature, they overlap effectively the data types contained in other programming languages, giving then to Php an unprecedented flexibility in tabular operation processing.

$a             = array('Mario''Luigi');
$a[]           = 'Toad';
$a['Princess'] = 'Peach';
$a[]           = 'Koopa';
$a[5]          = 'Lakitu';
$a[]           = 'Bowser';
 
foreach($a as $key => $value) {
  echo $key.' => '.$value."\n";
}
 
// The result is :
// ---------------
// 0 => Mario
// 1 => Luigi
// 2 => Toad
// Princess => Peach
// 3 => Koopa
// 5 => Lakitu
// 6 => Bowser

The procedural and object orientation

Php is able to interprete scripts which syntax is both procedural and object, even if styles are mixed in a single file. The object orientation, available since Php 4, has been significantly extended with Php 5.

class A {
  protected $a;
 
  public function __construct() {
    $this->a = 42;
  }
  public function getA() {
    return $this->a;
  }
}
 
class extends A {
  public function __construct() {
    parent::__construct();
  }
}
 
$a = new A();
echo $a->getA(); // Display 42.
 
$b = 'B';
$b = new $b();
echo $b->getA(); // Display 42.
 
$a = null;       // Free memory.
$b = null;

The magic methods and functions

The intelligence of Php is also based on the use of methods and functions so-called « magic ». With these chatacteristics, Php is able to load dynamically classes, and intercept automatically calls to inexistent methods. By using this feature, we can build on the fly getters and setters.

// We are here : ./classes/EntityObject.php
 
abstract class EntityObject {
  /**
   * Construct the class.
   */
  public function __construct() {
  }
  /**
   * Destroy the class.
   */
  public function __destruct() {
  }
  /**
   * Intercept calls on accessors (getters and setters)
   * and dynamically create them.
   */
  public function __call($method, $attrs) {
    $prefix = substr($method, 0, 3);
    $suffix = chr(ord(substr($method, 3, 1)) + 32);
    $suffix .= substr($method, 4);
    $cattrs = count($attrs);
    if (property_exists($this, $suffix)) {
      if ($prefix === 'set') {
        return $this->__set($suffix, $attrs[0]);
      }
      if ($prefix === 'get') {
        return $this->__get($suffix);
      }
    }
    trigger_error('The method '.$method.' does not exist.');
  }
  /**
   * Get the value of the given member.
   */
  public function __get($member) {
    return $this->{$member};
  }
  /**
   * Set the given value to the given member.
   */
  public function __set($member, $value) {
    $this->{$member} = $value;
  }
}

// We are here : ./
 
/**
 * Load automatically classes definition.
 * This is a kind of file auto-inclusion.
 */
function __autoload($className) {
  require_once './classes/' . $className . '.php';
}
 
class MyObject extends EntityObject {
  protected $a;
  protected $b;
 
  public function getB() {
    return $this->b + 1;
  }
}
 
$myObject = new MyObject();
$myObject->setA(2);
$myObject->setB(1);
echo $myObject->getA(); // Display 2.
echo $myObject->getB(); // Display 2.
$myObject = null;

An unbelievable ease

One of the main ideas of Php is the accessibility to everyone. This is transposed by a language accessible to both novices and skilled developpers. Another thing that makes Php remarkable is the simplicity of interaction with third-features, such as databases with Pdo, Xml files with SimpleXml, ...etc. Considering all programming languages, Php is by far the most interesting that I had to approach.

[Page generated in 0.012094020843506 second]