Índice

Basics

class A {
   function foo(){
      if (isset($this)) {
         echo '$this is defined ('.get_class($this).")\n";
      }
   }
}
abstract class A {
   abstract private function foo();
}
interface A extends B,C {  // interfaces can extend more than 1 interfaces
   public function foo($var);
   public function foo2($var,$var2);
}
trait traitExample {
   function a() { … }
}
class A {
   use traitExample, traitExample2;  // should not have same method names, otherwise use 'as' for conflict resolutions
   ...
}
$instance = new A(); // variable name allowed e.g. $class='A';… new $class()…
$instance_b = new $instance; // allowed from PHP 5.3
class ExtendClass extends BaseClass {
   echo "This class is extended";
   parent::displayVar();
}
$copy = clone $object;

Properties (class variables)

public $var1 = myConstant;
public $var2 = array(true, false);
public $var3 = <<<'EOD'  // nowdoc is allowed only in PHP 5.3.0 and later
hello world
EOD;
class MyClass{
   public static $myPro = 12343;
}
print MyClass::$myPro;
const myConstant = 'constant value';

Methods (class functions)

Property Overloading (must be declared public)

Method Overloading (must be declared public)

Magic Methods

Comparing Objects

Typehinting

function test(stdClass $obj = NULL) { … }
test(NULL);
test(new stdClass);

Late Static Bindings

class A {
   public static function who() {
      echo __CLASS__;
   }
   public static function test() {
      static::who(); // Here comes Late Static Bindings
   }
}

class B extends A {
   public static function who() {
      echo __CLASS__;
   }
}

B::test(); // gives B; if self is used, gives A

Instanceof / is_a()

Object Iteration

class MyClass
{
   public $var1 = 'value 1';
   protected $protected = 'protected var';
   private $private   = 'private var';
}

$class = new MyClass();
foreach($class as $key => $value) {
   print "$key => $value\n";  // output var 1=> value 1
}

Exceptions

try {
   $o = new TestException(TestException::THROW_DEFAULT);
} catch (MyException $e) {      // Doesn't match this type
   echo "Caught my exception\n", $e;
   $e->customFunction();
} catch (Exception $e) {        // Will be caught
   echo "Caught Default Exception\n", $e;
}
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
   throw new ErrorException($errstr, $errno, 0, $errfile, $errline); //0:severity
}
set_error_handler("exception_error_handler");

/* Trigger exception */
strpos();
Exception {
   /* Properties */
   protected string $message ;
   protected int $code ;
   protected string $file ;
   protected int $line ;
   /* Methods */
   public __construct ([ string $message = "" [, int $code = 0 [, Exception $previous = NULL ]]] )
   final public string getMessage ( void ) // get error message
   final public Exception getPrevious ( void ) // previous error
   final public mixed getCode ( void ) // get error code
   final public string getFile ( void ) // get filename
   final public int getLine ( void ) // get the line
   final public array getTrace ( void ) // get the stack trace (array with all)
   final public string getTraceAsString ( void ) // stack trace as string
   public string __toString ( void ) // when echo $e used
   final private void __clone ( void ) // fatal error
}

Generators (only in PHP > 5.5)

Predefined Interfaces

Reflection

$obj = new ReflectionFunction('strstr');
foreach( $obj->getParameters() as $param ){
   print $param;
} // output: Parameter #0 [ $haystack ] Parameter #1 [ $needle ]

SPL

$array = array('koala', 'kangaroo', 'wombat', 'wallaby', 'emu');
$object = new ArrayIterator($array);
for ( $object->rewind(); $object->valid(); $object->next() )
{
   if($object->current() === 'kiwi')
   {
      $object->offsetUnset( $object->key() );
   }
   echo $object->key().' - '.$object->offsetGet($object->key())."\n";
}
Flags: ArrayObject::STD_PROP_LIST, ArrayObject::ARRAY_AS_PROPS
class Example {
   public $public = 'prop:public';
   private $prv   = 'prop:private';
   protected $prt = 'prop:protected';
}
$arrayobj = new ArrayObject(new Example());
var_dump($arrayobj->count()); // int(1), only public properties
$arrayobject = new ArrayObject($array);
$iterator = $arrayobject->getIterator();
while($iterator->valid()) {
   echo $iterator->key() . ' => ' . $iterator->current() . "\n";
   $iterator->next();
}

SPL Autoload

<?php
function __autoload($class_name)
{
   require_once "/www/phpClasses/{$class_name}.inc.php";
}
$a = new friend;