<?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;&#10;&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;&#10;&lt;pre&gt;&lt;code&gt;// Call with php -f test.php&#10;&#10;error_reporting(E_ALL);&#10;&#10;// Class (blueprint) = package =&amp;gt; object (instantiated class)&#10;class ParentClass {&#10;&#10; // variable = property&#10;&#10; // encapsulation&#10; // visible anywhere&#10; public $public = 'Public';&#10; // visible only within the class, and in inherited and parent classes&#10; protected $protected = 'Protected';&#10; // visible only within class&#10; private $private = 'Private';&#10;&#10; // function = methode&#10; public function printItem($string) {&#10;&#10; // ParentClass&#10; print get_class($this) . ': ' . $string . PHP_EOL;&#10; print $this-&amp;gt;public . PHP_EOL;&#10; print $this-&amp;gt;protected . PHP_EOL;&#10; print $this-&amp;gt;private . PHP_EOL;&#10; }&#10;&#10; const CONST_VALUE = 'PHP is great!';&#10;&#10; public function printPHP() {&#10;&#10; $classname = get_class($this);&#10; print constant($classname . '::CONST_VALUE') . PHP_EOL;&#10; print $classname::CONST_VALUE . PHP_EOL;&#10; print ParentClass::CONST_VALUE . PHP_EOL;&#10; }&#10;}&#10;&#10;class ChildClass extends ParentClass {&#10;&#10; public function printItem($string) {&#10;&#10; // ChildClass&#10; print get_class($this) . ': ' . $string . PHP_EOL;&#10; print self::__bla();&#10; }&#10;&#10; // private functions are names with __&#10; private function __bla() {&#10; print 'bla' . PHP_EOL;&#10; }&#10;}&#10;&#10;class Main {&#10;&#10; private static $instance;&#10; var $bla;&#10;&#10; private static function __init() {&#10; }&#10;}&#10;&#10;class Person {&#10;&#10; var $name; // public!&#10;&#10; // access modfiers (public = default)&#10; public $height;&#10; protected $social_insurance;&#10; private $pin_number;&#10;&#10; // PHP constructor methode initialise object when instantiated&#10; function __construct($persons_name) {&#10;&#10; $this-&amp;gt;name = $persons_name;&#10; }&#10;&#10; private function getPinNumber() {&#10;&#10; return $this-&amp;gt;pin_number;&#10; }&#10;&#10; function setName($new_name) {&#10;&#10; $this-&amp;gt;name = $new_name;&#10; }&#10;&#10; function getName() {&#10;&#10; return $this-&amp;gt;name;&#10; }&#10;}&#10;&#10;// inherit from Person all public and protected properties and methodes&#10;// Employee is a TYPE of Person&#10;class Employee extends Person {&#10;&#10; function __construct($employee_name) {&#10;&#10; $this-&amp;gt;setName($employee_name);&#10; }&#10;&#10; // Overwrite the setName methode from Person&#10; function setName($new_name) {&#10; if ( $new_name != 'Oli' ) {&#10; $this-&amp;gt;name = strtoupper($new_name);&#10; }&#10; else {&#10; Person::setName($new_name);&#10; // Or alternatively:&#10; // parent::setName($new_name);&#10; }&#10; }&#10;}&#10;&#10;&#10;$main = new Main();&#10;&#10;$parent = new ParentClass();&#10;$child = new ChildClass();&#10;&#10;$parent-&amp;gt;printItem('baz');&#10;// Output: 'ParentClass: baz'&#10;$parent-&amp;gt;printPHP();&#10;// Output: 'PHP is great'&#10;&#10;$child-&amp;gt;printItem('baz');&#10;// Output: 'ChildClass: baz'&#10;$child-&amp;gt;printPHP();&#10;// Output: 'PHP is great'&#10;&#10;&#10;// interface bar {&#10;// }&#10;&#10;// class MyDatabase implements IDatabase {&#10;// }&#10;&#10;// abstract class Base {&#10;// abstract protected function __construct ();&#10;// }&#10;&#10;// protected function hello_right () {&#10;&#10;// handle/reference to new object person&#10;$p = new Person('Oliver');&#10;print $p-&amp;gt;getName() . PHP_EOL;&#10;$p-&amp;gt;setName('Oli');&#10;print $p-&amp;gt;getName() . PHP_EOL;&#10;// This is a bad behaviour!&#10;print 'Bad hehaviour: ' . $p-&amp;gt;name . PHP_EOL;&#10;// This should give an error&#10;// print $p-&amp;gt;pin_number . PHP_EOL;&#10;&#10;&#10;$e = new Employee('Hugo');&#10;print &amp;quot;Employee &amp;quot; . $e-&amp;gt;getName() . PHP_EOL;&#10;$e = new Employee('Oli');&#10;print &amp;quot;Employee &amp;quot; . $e-&amp;gt;getName() . PHP_EOL;&#10;&#10;function inverse($x) {&#10; if (!$x) {&#10; throw new Exception('Division by zero.');&#10; }&#10; else return 1/$x;&#10;}&#10;&#10;try {&#10; echo inverse(5) . &amp;quot;&amp;lt;br&amp;gt;n&amp;quot;;&#10; echo inverse(0) . &amp;quot;&amp;lt;br&amp;gt;n&amp;quot;;&#10;}&#10;catch (Exception $e) {&#10; echo 'Caught exception: ' . $e-&amp;gt;getMessage() . &amp;quot;&amp;lt;br&amp;gt;n&amp;quot;;&#10;}&#10;&#10;// Continue execution&#10;echo 'Hello World';&#10;*/&#10;&lt;/code&gt;&lt;/pre&gt;</description></item></channel></rss>