ObjectOrientation

Created by: Anonymous, Last modification: 11 Mar 2006 (11:43 UTC) by Uwe Zimmermann
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:

<?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:

<?php
$quickTest 
= new MyClass();
?>

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:

<?php
$quickTest 
= new MyClass();
echo 
"Whats the value: ".$quickTest->getText()."<br/>";
?>


Whats the value: not-set

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:

<?php
$quickTest 
= new MyClass();
echo 
"Whats the value: ".$quickTest->getText()."<br/>";
$x "some new text";
echo 
"Set the value: ".$x."<br/>";
$quickTest->setText($x);
echo 
"Whats the value: ".$quickTest->getText()."<br/>";
?>


Whats the value: not-set
Set the value: some new text
Whats the value: some new text

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 not include presentation code. Take a look at Model View Controller - MVC to understand where the data classes and libraries (the Model) fit into the scheme of things. Also, dependancies on libraries and base classes should be refined and minimal.

Also have a look at ClassStructure for a more detailed look into the use of classes and OO in bitweaver.

If you are trying to get kernel/test/ working, you'll need to do these two things:
  1. Move your kernel/adodb/ directory to util/adodb/. HEAD code will continue to work, so don't get worried yet.
  2. In your config_inc.php file, place a URI:
$dbSettings = "dbtype://user:pass@mydb/dbname";
where dbtype is the Adodb driver names or abbreviations - mysql, mssql, pgsql, sybase, oci, firebird15, etc.