Related

Elsewhere

PHP has a full object oriented feature set.  However many dynamic web pages interface with other resources to provide persistent rich content.  Efficient PHP may leverage databases and provide simple output to client side javascript without needing to engage in heavy object oriented structures for basic functions.  Please use appropriately.

Traditionally properties defined using var are public (external code can cite class instance and property to access without using a class method).  Additional property visibilities are public, protected, and private.  Private properties are not inheretid by child classes, while public and protected propeties are.

Inside class code useis this-> as prefix to refer to properties and methods, outside code uses class ‘instance’->.  You can also access public constant properties and methods without an instance using class::method, (i.e. double colon) nomenclature.  Be careful, there are also ::parent, ::self, ::static prefixes that have narrowly defined applications.

Methods have public visibility by default.  Optionally, prefixing abstract to a class declaration insures no direct instance can be made, but it can be used as template to extend other classes to enforce certain parameters are consistent between different extended classes.

<?php

//  utility debug functions
function print_vars($obj) {
    foreach (
get_object_vars($obj) as $prop => $val)
        echo 
"<br>\t$prop = $val\n";
    }

function 
print_methods($obj) {
    
$arr get_class_methods(get_class($obj));
    foreach (
$arr as $method)
        echo 
"<br>\tfunction $method()\n";
    }

function 
class_parentage($obj$class) {
    if (
is_subclass_of($GLOBALS[$obj], $class)) {
        echo 
"\n<br>Object $obj belongs to class ".get_class($$obj);
        echo 
" a subclass of $class\n";
        }
    else
        echo 
"\n<br>Object $obj does not belong to a subclass of $class\n";
    }

//  base class: Vegetable
class Vegetable {
    var 
$edible;  //  1st property
    
var $color;   //  2nd property

    
function Vegetable($edible$color="green") {
        
//  constructor method, called automatically for each new instance
        
$this->edible $edible;
        
$this->color $color;
        }

    function 
is_edible() {   //  1st custom method, return edible state
        
return $this->edible;
        }

    function 
what_color() {  //  2nd custom method, return color
        
return $this->color;
        }
    }

// extended class: Spinach (extends Vegetable class)
class Spinach extends Vegetable {
    var 
$cooked false;

    function 
Spinach() {
        
//  constructor method, called automatically for each new instance
        
$this->Vegetable(true"green");
        }

    function 
cook_it() {   //  1st custom method, forces cooked value
        
$this->cooked true;
        }

    function 
is_cooked() { //  2nd custom method, return cooked state
        
return $this->cooked;
        }
    }

//  object orientend programming demonstration

//  instantiate base class Vegetable: veg_std (ediable state MUST be provided upon creation)
//  provide override values for edible: true, color: blue, for constructor method
$veg_std = new Vegetable(true"blue");

//  instantiate extended class Spinach: veg_spinach
//  no override values provided, the Spinach constuctor will adjust base class properties edible: true, color: green
$veg_spinach = new Spinach();

$veg_spinach->cook_it();

// show information about objects
echo "\n<p>get_class(veg_std): ".get_class($veg_std);
echo 
"\n<br>get_parent_class(veg_std): ".(get_parent_class($veg_std) ? get_parent_class($veg_std) : '[no parent]');
echo 
"\n<br>get_class(veg_spinach): ".get_class($veg_spinach);
echo 
"\n<br>get_parent_class(veg_spinach): ".(get_parent_class($veg_spinach) ? get_parent_class($veg_spinach) : '[no parent]');
echo 
"</p>";

// show veg_std properties
echo "\n<p>veg_std, properties\n";
print_vars($veg_std);
echo 
"</p>";

// show veg_spinach methods
echo "\n<p>veg_spinach: methods\n";
print_methods($veg_spinach);
echo 
"</p>";

// show object parentage
echo "\n<p>Parentage:\n";
class_parentage("veg_spinach""Spinach");
class_parentage("veg_spinach""Vegetable");
echo 
"</p>";
?>

Output

get_class(veg_std): Vegetable
get_parent_class(veg_std): [no parent]
get_class(veg_spinach): Spinach
get_parent_class(veg_spinach): Vegetable

veg_std, properties
edible = 1
color = blue

veg_spinach: methods
function Spinach()
function cook_it()
function is_cooked()
function Vegetable()
function is_edible()
function what_color()

Parentage:
Object veg_spinach does not belong to a subclass of Spinach
Object veg_spinach belongs to class a subclass of Vegetable