Dup Ver Goto 📝

PHP Class Cheat Sheet

To
45 lines, 158 words, 1196 chars Page 'ClassCheatSheet' does not exist.

The general idea of a cheat sheet is to give minimal examples, enough to remember things. For example the syntax for one class deriving from another is different between C++, Java, Python and PHP.

<?php

abstract class Base {
    function hello(): void {
        echo "hello Base\n";
    }
    function am_I_happy(int $wealth, string $home): bool { 
        return true; 
    }
    abstract function boing(): int;
}
class MyClass extends Base {
    public $a;
    private $b;
    function boing(): int {
        return 0;
    }
    private function snarf(): bool {
        return true;
    }
    function name(): string { 
        return "Mr Flibble"; 
    }
    function getmyname(): string {
        return "My name is ".$this->name();
    }
}
$a = new MyClass();
$a->hello();                   # use -> to refer to members
if( $a->am_I_happy(0,"") ) {
  echo "happy\n";
}
$fname = "hello";
$a->$fname();                  # we can use strings to refer to member functions
if( method_exists($a,$fname) ) {
  $a->$fname();
}
$x = "hello NAME, are you NAME?";
$y = preg_replace_callback('/N\w*E/',[$a,"name"],$x);
echo "$x\n$y\n";