You are here

PHP cheat sheet

PHP Java bytecode

PHP Compiler

// Call with php -f test.php

error_reporting(E_ALL);

// Class (blueprint) = package => object (instantiated class)
class ParentClass {

  // variable = property

  // encapsulation
  // visible anywhere
  public $public       = 'Public';
  // visible only within the class, and in inherited and parent classes
  protected $protected = 'Protected';
  // visible only within class
  private $private     = 'Private';

  // function = methode
  public function printItem($string) {

    // ParentClass
    print get_class($this) . ': ' . $string . PHP_EOL;
    print $this->public . PHP_EOL;
    print $this->protected . PHP_EOL;
    print $this->private . PHP_EOL;
  }

  const CONST_VALUE = 'PHP is great!';

  public function printPHP() {

    $classname = get_class($this);
    print constant($classname . '::CONST_VALUE') . PHP_EOL;
    print $classname::CONST_VALUE . PHP_EOL;
    print ParentClass::CONST_VALUE . PHP_EOL;
  }
}

class ChildClass extends ParentClass {

  public function printItem($string) {

    // ChildClass
    print get_class($this) . ': ' . $string . PHP_EOL;
    print self::__bla();
  }

  // private functions are names with __
  private function __bla() {
    print 'bla' . PHP_EOL;
  }
}

class Main {

  private static $instance;
  var $bla;

  private static function __init() {
  }
}

class Person {

  var $name;   // public!

  // access modfiers (public = default)
  public $height;
  protected $social_insurance;
  private $pin_number;

  // PHP constructor methode initialise object when instantiated
  function __construct($persons_name) {

    $this->name = $persons_name;
  }

  private function getPinNumber() {

    return $this->pin_number;
  }

  function setName($new_name) {

    $this->name = $new_name;
  }

  function getName() {

    return $this->name;
  }
}

// inherit from Person all public and protected properties and methodes
// Employee is a TYPE of Person
class Employee extends Person {

  function __construct($employee_name) {

    $this->setName($employee_name);
  }

  // Overwrite the setName methode from Person
  function setName($new_name) {
    if ( $new_name != 'Oli' ) {
      $this->name = strtoupper($new_name);
    }
    else {
      Person::setName($new_name);
      // Or alternatively:
      // parent::setName($new_name);
    }
  }
}


$main = new Main();

$parent = new ParentClass();
$child = new ChildClass();

$parent->printItem('baz');
// Output: 'ParentClass: baz'
$parent->printPHP();
// Output: 'PHP is great'

$child->printItem('baz');
// Output: 'ChildClass: baz'
$child->printPHP();
// Output: 'PHP is great'


// interface bar {
// }

// class MyDatabase implements IDatabase {
// }

// abstract class Base {
//     abstract protected function __construct ();
// }

// protected function hello_right () {

// handle/reference to new object person
$p = new Person('Oliver');
print $p->getName() . PHP_EOL;
$p->setName('Oli');
print $p->getName() . PHP_EOL;
// This is a bad behaviour!
print 'Bad hehaviour: ' . $p->name . PHP_EOL;
// This should give an error
// print $p->pin_number . PHP_EOL;


$e = new Employee('Hugo');
print "Employee " . $e->getName() . PHP_EOL;
$e = new Employee('Oli');
print "Employee " . $e->getName() . PHP_EOL;

function inverse($x) {
  if (!$x) {
    throw new Exception('Division by zero.');
  }
  else return 1/$x;
}

try {
  echo inverse(5) . "\n";
  echo inverse(0) . "\n";
}
catch (Exception $e) {
  echo 'Caught exception: ' .  $e->getMessage() . "\n";
}

// Continue execution
echo 'Hello World';
*/
tags: