39. OO in PHP - basic
<?php
class SimpleClass
{
// property declaration
public $var = 'a default value';
// method declaration
public function displayVar() {
echo $this->var;
}
}
$instance = new SimpleClass;
$instance->displayVar();
40. OO in PHP – ctor / dtor
<?php
class CtorTestClass
{
function __construct ($msg = ‘’) {
echo “constructed, msg = $msg.”;
}
function __destruct () {
echo ‘destructed.’;
}
}
$instance = new CtorTestClass;
$instance1 = new CtorTestClass(‘haha’);
41. OO in PHP – inheritance
<?php
class Animal {
public $name;
function __construct ($name) {
$this->name = $name;
}
}
class Cat extends animal {
public function meow () {
echo $this->name . ‘: meow’;
}
}
$cat = new Cat(‘useless’);