resources

Modern PHP

server components

  • hhvm
  • opcache
  • mysqlnd
  • Enchant - An abstraction layer above various spelling libraries
  • Fileinfo - An improved and more solid replacement, featuring full BC, for the Mimetype extension, which has been removed.
  • INTL - Internationalization extension. INTL is a wrapper around the » ICU library.
  • Phar - Implementation of PHP-Archive files.
  • SQLite3 - Support for SQLite version 3 databases.
  • phpdbg

namespaces

namespace foo {
    use 
My\Full\Classname as Another;

    
// this is the same as use My\Full\NSname as NSname
    use My\Full\NSname;

    
// importing a global class
    use ArrayObject;

    
$obj = new namespace\Another// instantiates object of class foo\Another
    $obj = new Another// instantiates object of class My\Full\Classname
    NSname\subns\func(); // calls function My\Full\NSname\subns\func
    $a = new ArrayObject(array(1)); // instantiates object of class ArrayObject
    // without the "use ArrayObject" we would instantiate an object of class foo\ArrayObject
}
namespace {
    
// global
}

traits

trait SayWorld {
    public function 
sayHello() {
        
parent::sayHello();
        echo 
'World!';
    }
}
class 
MyHelloWorld extends Base {
    use 
SayWorld;
}

variadic functions variable parameters

function f($req$opt null, ...$params) {
    
printf('$req: %d; $opt: %d; number of params: %d'."\n",$req$optcount($params));
}

tips

controllare quali files php.ini siano caricati e concorrano alla configurazione :

php -i | grep php.ini

display all errors

error_reporting(E_ALL);
ini_set('display_errors','On');
$log_file APPLICATION_PATH."/../var/logs/php_error.log";
ini_set('error_log'$log_file);
ini_set("display_startup_errors""1");

SPL

serializzare

class Base implements Serializable {
  private 
$baseVar;

  public function 
__construct() {
    
$this->baseVar 'foo';
  }

  public function 
serialize() {
    return 
serialize($this->baseVar);
  }

  public function 
unserialize($serialized) {
    
$this->baseVar unserialize($serialized);
  }

  public function 
printMe() {
    echo 
$this->baseVar "\n";
  }
}

autoload

if(false === spl_autoload_functions()) {
  if(
function_exists('__autoload')) {
    
spl_autoload_register('__autoload',false);
  }
}
//Continue to register autoload functions
spl_autoload_register(null,false);
spl_autoload_extensions('.php,.inc,.class,.interface');
function 
myLoader1($class) {
  
//Do something to try to load the $class
}
function 
myLoader2($class) {
  
//Maybe load the class from another path
}
spl_autoload_register('myLoader1',false);
spl_autoload_register('myLoader2',false);
$test = new SomeClass();

//Try to load className.php
if(spl_autoload_call('className')
  && 
class_exists('className',false)
  ) {

  echo 
'className was loaded';

  
//Safe to instantiate className
  $instance = new className();
} else {

  
//Not safe to instantiate className
  echo 'className was not found';

}

confrontare oggetti con HASH

class {}
$instance = new a();
echo 
spl_object_hash($instance);

SPL Observer

class DemoSubject implements SplSubject {

  private 
$observers$value;

  public function 
__construct() {
    
$this->observers = new SplObjectStorage();
  }

  public function 
attach(SplObserver $observer) {
    
$this->observers->attach($observer);
  }

  public function 
detach(SplObserver $observer) {
    
$this->observers->detach($observer);
  }

  public function 
notify() {
    foreach(
$this->observers as $observer) {
      
$observer->update($this);
    }
  }

  public function 
setValue($value) {
    
$this->value $value;
    
$this->notify();
  }

  public function 
getValue() {
    return 
$this->value;
  }

}

class 
DemoObserver implements SplObserver {

  public function 
update(SplSubject $subject) {
    echo 
'The new value is '$subject->getValue();
  }

}

$subject = new DemoSubject();
$observer = new DemoObserver();
$subject->attach($observer);
$subject->setValue(5);

iteretors

iteretor XML

$it = new SimpleXMLIterator(file_get_contents('test.xml'));

foreach(
$it as $key=>$node) {
  echo 
$key "\n";
  if(
$it->hasChildren()) {
    foreach(
$it->getChildren() as $element=>$value) {
      echo 
"\t"$element ":" $value ."\n";
    }
  }
}
$xml simplexml_load_file('MyXMLFile.xml');
foreach (
$xml->employee as $e) {
    echo 
"First name: "$e->firstname"\n";
}

iterator Directory

$dir '/path/to/plugins';
$dirit = new DirectoryIterator($dir);

foreach(
$dirit as $file) {
  if(!
$file->isDir()) { //Ignore directories, e.g., ./ and ../
    require_once($file);
  }
}

text file iteration

$it = new SplFileObject('pm.csv');

foreach(
$it as $line) {
        echo 
$line;
}

oggetto accessibile come Array

class MyArray implements ArrayAccess {

  protected 
$_arr;

  public function 
__construct() {
    
$this->_arr = array();
  }

  public function 
offsetSet($offset$value) {
    
$this->_arr[$offset] = $value;
  }

  public function 
offsetGet($offset) {
    return 
$this->_arr[$offset];
  }

  public function 
offsetExists($offset) {
    return 
array_key_exists($offset$this->_arr);
  }

  public function 
offsetUnset($offset) {
    unset(
$this->_arr[$offset]);
  }

}

$myArray = new MyArray();   // Create an object as an array
$myArray['first'] = 'test'// offsetSet, set data by key
$demo $myArray['first'];  // offsetGet, get data by key
unset($myArray['first']);   // offsetUnset, remove key

oggetto lista, accessibile come array e iterabile con foreach

class KeyObject {}

class 
CollectionObject extends ArrayIterator implements ArrayAccess {

  protected 
$_keys$_values;

  public function 
__construct() {
    
$this->_keys = array();
    
$this->_values = array();
    
parent::__construct(&$this->_values);
  }

  public function 
offsetSet($key$value) {
    
$this->_keys[spl_object_hash($key)] = $key;
    
$this->_values[spl_object_hash($key)] = $value;
  }

  public function 
offsetGet($key) {
    return 
$this->_values[spl_object_hash($key)];
  }

  public function 
offsetExists($key) {
    return 
array_key_exists(spl_object_hash($key), $this->_values);
  }

  public function 
offsetUnset($key) {
    unset(
$this->_values[spl_object_hash($key)]);
  }

  public function 
getKey($hash) {
    return 
$this->_keys[$hash];
  }

}

$key = new KeyObject();
$collection = new CollectionObject();
$collection[$key] = 'test';

foreach(
$collection as $k => $v) {
  
print_r($collection->getKey($k));
  
print_r($v);
}

file IO

$file = new SplFileObject($logfile,'a+');
  
$file->fwrite($le->__toString());