Cheatography
https://cheatography.com
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 showConstant() { echo self::CONSTANT . "\n"; } echo $classname::CONSTANT |
$this-> |
extends |
class SubClass extends BaseClass {} |
var |
function |
__construct() |
function __construct() {} |
parent:: |
parent::__construct(); |
|
|
|
|
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::CONST_VALUE |
|