Show Menu
Cheatography

PHP OOP Cheat Sheet (DRAFT) by

PHP Object oriented programming syntax

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Basic Syntax

class SimpleClass
   
    public $var = 'a default value';

    public function displayVar() {
        echo $this->var;
    }
}


$instance = new SimpleClass();


$className = 'SimpleClass';
$instance = new $className();

keywords

public class
static
new
const
const CONSTANT = 'constant value';
private const
public const
self::
function showCo­nst­ant() { echo self::­CON­STANT . "­\n"; }
echo $class­nam­e::­CON­STANT
$this->
extends
class SubClass extends BaseClass {}
var
function
__cons­truct()
function __cons­truct() {}
parent::
parent­::_­_co­nst­ruct();
   

Visibility

public
can be accessed everywhere
protected
can be accessed only within the class itself and by inherited classes
private
may only be accessed by the class that defines the member
var
var === public
::
access to static, constant, and overridden properties or methods of a class
self::
self::­$my­_static
parent::
parent­::C­ONS­T_VALUE