Version 3


Created by: Stephan Borg, Last modification: 22 Aug 2004 (00:35 UTC) by Stephan Borg
This is supposed to be a quick explanation on Object Orientation (at least how I interpret it).

Object Orientation or 'Black Box' design, basically means each class is designed so:
  1. you only need to know what goes into it and
  2. what should come out of it

Here is a simple PHP object to help explain how this works:
{CODE(colors=>php)}class MyClass {
// internal variable - should never be accessed
// externally.
var mText;
// initialisation - set default text to "not-set"
function MyClass() {
$this->mText = "not-set";
// get the text using this function
function getText() {
return $this->mText;
// set the text using this function
function setText($x) {
$this->mText = $x;
The first internal variable you can see is mText. It is recommended in Object Orientation (OO) that you (almost) never access internal variables except from within the class itself. Hence will never need to know the internals of the class and if developers don't go changing the internal variables, everything is a lot easier to control.

Secondly, if everything goes through functions, you can validate data going in to the class and make sure its not going to break things.

The first function we see is MyClass(). This is a constructor or initialisation function. This function is always called when you create a new instance of this class. Below is how you create an instance:
{CODE(colors=>php)}$quickTest = new MyClass();{CODE}
Simple hey. Now, we want to see the text inside the class. Although you cannot hide the internal variables in PHP4, PHP5 has introduced methods to prevent users accessing internal variables. So I can only emphasise - don't access the internal variables directly. So we've created the class, lets have a look whats inside:
{CODE(colors=>php)}$quickTest = new MyClass();
echo "Whats the value: ".$quickTest->getText()."<br/>";{CODE}
{CODE()}Whats the value: not-set{CODE}
So we can see that the default value, which is set during initialisation is there.
Next, we want to set the text with something new. Again (I can't emphasise this enough) - don't access the internal variables directly. So here's how we do it:
{CODE(colors=>php)}$quickTest = new MyClass();
echo "Whats the value: ".$quickTest->getText()."<br/>";
$x = "some new text";
echo "Set the value: ".$x."<br/>";
echo "Whats the value: ".$quickTest->getText()."<br/>";{CODE}
{CODE()}Whats the value: not-set
Set the value: some new text
Whats the value: some new text{CODE}
This is a really simple example, but it is the essence of how OO works. have a look at TestingSuites, which rely on this methodology to work.

When you start designing classes, contrary to the code above, you should never include presentation code. Take a look at ModelViewController to understand where the data classes and libraries (the Model) fit into the scheme of things.

Also have a look at ClassStructure for a more detailed look into the use of classes and OO in TikiPro.
Page History
11 Mar 2006 (11:43 UTC)
Uwe Zimmermann87.96.133.2195
Current • Source
Stephan Borg218.214.1.1134
View • Compare • Difference • Source
Stephan Borg218.214.1.1133
View • Compare • Difference • Source
Stephan Borg218.214.1.1132
View • Compare • Difference • Source