This it the real meat and potatoes of PHP. Classes contain methods (functions) and properties (variables). This basic concept is also known as encapsulation or information hiding.
It’s important to note that when you create an instance of a class, passing it to functions or creating new variables do not make copies of the object, but instead create references to it.
-
public $value;
-
}
-
-
$test = new tempClass();
-
$test->value = 5;
-
-
$new = $test;
-
$new->value = 10;
-
-
echo $test->value; // Displays 10
Since I just showed you above, classes can contain properties as well as methods. Here is an example of how to create and reference them.
-
public $publicProperty;
-
-
function basicFunction() {
-
echo "Function 1, ";
-
$this->otherFunction();
-
}
-
-
function otherFunction() {
-
echo "Function 2, ";
-
echo $this->publicProperty;
-
}
-
}
-
-
$instance = new importantClass();
-
$instance->publicProperty = 5;
-
$instance->basicFunction();
-
// Displays: Function 1, Function 2, 5
Notice the use of $this. You HAVE to use that to reference local variables and functions. You can also use :: to reference the class name itself, usually this is only used when extending classes.
-
function display() {
-
echo "a::display()";
-
}
-
}
-
class b extends a {
-
function test() {
-
a::display();
-
}
-
function display() {
-
parent::display();
-
}
-
}
Only because class b extends a can you reference the display method that way. And since b extends a, calling parent::display() will call a::display(). A little confusing, I know. But as an example, you can then create a class called c which extends b, and therefore can call a::display().
Now let’s say we want to assign some values or perform some functionality when creating or destroying an object, we can define those operations in the __construct and __destruct methods.
-
public $width;
-
public $height;
-
-
function __construct($w, $h) {
-
$this->width = $w;
-
$this->height = $h;
-
}
-
}
-
-
$square = new box(5, 5);
-
echo $square->width; // Displays 5
But what if you wanted to prevent the user from accessing properties, calling functions, or even extending classes? You could hide them with the visibility (a.k.a. PPP):
- Public: accessed from any scope
- Protected: accessed from within the class where it is defined and any descendants (extended classes).
- Private: accessed only from within the class where it is defined.
- Final: accessible from any scope but cannot be overridden in descendants.
Final can only be applied to methods and classes, a final class cannot be extended. Here is an example:
-
public $a;
-
protected $b;
-
private $c;
-
-
function display() {
-
echo implode(',', array_keys(get_object_vars($this))));
-
}
-
}
-
-
class bar extends foo {
-
function display() {
-
echo implode(',', array_keys(get_object_vars($this))));
-
}
-
}
-
-
$test1 = new foo();
-
$test1->display(); // Displays $a, $b, $c
-
-
$test2 = new foo();
-
$test2->display(); // Displays $a, $b
-
-
echo implode(',', array_keys(get_object_vars($test1)))); // Displays $a
Note the use of a new function get_object_vars($obj) which returns an associative array of all the properties available in the object.
Static functions and properties are ones that can be called/referenced/saved without an instance. These must be referenced with the :: syntax. Here is an example of how they work:
-
static $count = 0;
-
-
function __construct() {
-
$this::count ++;
-
}
-
function __destruct() {
-
$this::count –;
-
}
-
}
-
-
echo foo::count; // Displays 0
-
$a = new foo();
-
$b = new foo();
-
$c = new foo();
-
-
echo foo::count; // Displays 3
-
unset($a);
-
unset($b);
-
-
echo foo::count; // Displays 1
And finally, there are constants. Variables that are public, static, are declared with one value and cannot be changed.
Tomorrow I will be talking about Interfaces, Abstraction, Exceptions, and Reflection. Happy New Year!

