<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Php on FromDual GmbH</title><link>https://www.fromdual.com/tags/php/</link><description>Recent content in Php on FromDual GmbH</description><generator>Hugo</generator><language>en-GB</language><managingEditor>oli.sennhauser@fromdual.com (Oli Sennhauser)</managingEditor><webMaster>oli.sennhauser@fromdual.com (Oli Sennhauser)</webMaster><copyright>© FromDual GmbH</copyright><lastBuildDate>Thu, 09 Aug 2012 15:54:11 +0000</lastBuildDate><atom:link href="https://www.fromdual.com/tags/php/index.xml" rel="self" type="application/rss+xml"/><item><title>PHP cheat sheet</title><link>https://www.fromdual.com/blog/php-cheat-sheet/</link><pubDate>Thu, 09 Aug 2012 15:36:27 +0000</pubDate><author>oli.sennhauser@fromdual.com (Oli Sennhauser)</author><guid>https://www.fromdual.com/blog/php-cheat-sheet/</guid><description>&lt;p&gt;&lt;a href="http://www.caucho.com/resin-3.0/quercus/index.xtp" target="_blank" title="PHP Java bytecode"&gt;PHP Java bytecode&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://stackoverflow.com/questions/1408417/can-you-compile-php-code" target="_blank" title="PHP Compiler"&gt;PHP Compiler&lt;/a&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;// Call with php -f test.php

error_reporting(E_ALL);

// Class (blueprint) = package =&amp;gt; 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-&amp;gt;public . PHP_EOL;
 print $this-&amp;gt;protected . PHP_EOL;
 print $this-&amp;gt;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-&amp;gt;name = $persons_name;
 }

 private function getPinNumber() {

 return $this-&amp;gt;pin_number;
 }

 function setName($new_name) {

 $this-&amp;gt;name = $new_name;
 }

 function getName() {

 return $this-&amp;gt;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-&amp;gt;setName($employee_name);
 }

 // Overwrite the setName methode from Person
 function setName($new_name) {
 if ( $new_name != 'Oli' ) {
 $this-&amp;gt;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-&amp;gt;printItem('baz');
// Output: 'ParentClass: baz'
$parent-&amp;gt;printPHP();
// Output: 'PHP is great'

$child-&amp;gt;printItem('baz');
// Output: 'ChildClass: baz'
$child-&amp;gt;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-&amp;gt;getName() . PHP_EOL;
$p-&amp;gt;setName('Oli');
print $p-&amp;gt;getName() . PHP_EOL;
// This is a bad behaviour!
print 'Bad hehaviour: ' . $p-&amp;gt;name . PHP_EOL;
// This should give an error
// print $p-&amp;gt;pin_number . PHP_EOL;


$e = new Employee('Hugo');
print &amp;quot;Employee &amp;quot; . $e-&amp;gt;getName() . PHP_EOL;
$e = new Employee('Oli');
print &amp;quot;Employee &amp;quot; . $e-&amp;gt;getName() . PHP_EOL;

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

try {
 echo inverse(5) . &amp;quot;&amp;lt;br&amp;gt;n&amp;quot;;
 echo inverse(0) . &amp;quot;&amp;lt;br&amp;gt;n&amp;quot;;
}
catch (Exception $e) {
 echo 'Caught exception: ' . $e-&amp;gt;getMessage() . &amp;quot;&amp;lt;br&amp;gt;n&amp;quot;;
}

// Continue execution
echo 'Hello World';
*/
&lt;/code&gt;&lt;/pre&gt;</description></item></channel></rss>