PHP has been around for years, but very fiew PHP hackers use the power of Object Oriented coding style. I found this odd and wrote this primer in hopes that it will help some of you to understand the workings of OOP.
We'll start with a basic example and move onto a class example I've actually used myself. This document will be more code then talk so have fun with it.
A class is the basic foundation of coding PHP in an OO style. Here's an example of a basic class:
<?php
class FooBar
{
/* The Functions that make up the methods in this class. */
function input($statement)
{
if ($statement == 'foo') {
$this->comment = 'bar';
}
else if ($statement == 'bar') {
$this->comment = 'foo';
}
else {
$this->comment = 'Neither foo, nor a bar'er be.';
}
}
function output()
{
echo $this->comment;
}
}
?>
The basics of the class are the class name (FooBar) and the methods (input and output). Now let's try to use the class:
<?php
/* some html setup */
echo '<html><head><title>Example</title></head><body>';
/* some include or require statement to include the class, this is an example */
require_once 'foobar.class';
/* spawn a new class session (creates an object): */
$text = new FooBar;
/* use the input method */
$text->input('foo');
/* use the output method */
$text->output();
/* We'll do another example just for fun down here... */
/* another hunk of html to break our stuff apart */
echo '<p> </p>';
$another_one = new FooBar();
$another_one->input('Jump Around');
$another_one->output();
echo '</body></html>';
exit;
?>
That's the extreme basics. Useles right? Well, this example is. OOP is however not. Let's take a look at a class example that I have used myself.
<?php
class Table
{
/* Inital setup for the table. */
function tableSetup($rows, $columns)
{
$this->rows = $rows;
$this->columns = $columns;
}
/* Build the table */
function makeTable()
{
echo '<table with="100%" border="1">';
/* loop through the row value to build rows */
for ($r=1; $r<=$this->rows; $r++) {
echo '<tr>';
/* inside each row, loop through the columns */
for ($c=1; $c<=$this->columns; $c++) {
echo '<td>'.$r.'/'.$c.'</td>'; // puts the row and column into the cell
}
echo '</tr>';
}
echo '</table>';
}
}
?>
Now, this class will build a table for us and fill each cell with the the values of $r and $c. Cell 1 in the first row would have the contents of "1/1". The 3rd cell of the 2nd row would have "3/2" as it's contents. Let's try it out:
<?php
/* some html setup */
echo '<html><head><title>Table Example</title></head><body>';
require_once 'table.class';
$table = new Table();
$table->tableSetup(3,4);
$table->makeTable();
echo '</body></html>';
exit;
?>
So... it's: $var = new Classname, $var->method($value); and your off! This is a pretty basic example but should give you some insight on how classes work. If needed a second of this OOP series will be published to talk about extending classes beyond this type of basic functionality. Until then, Have phun...
We'll start with a basic example and move onto a class example I've actually used myself. This document will be more code then talk so have fun with it.
A class is the basic foundation of coding PHP in an OO style. Here's an example of a basic class:
<?php
class FooBar
{
/* The Functions that make up the methods in this class. */
function input($statement)
{
if ($statement == 'foo') {
$this->comment = 'bar';
}
else if ($statement == 'bar') {
$this->comment = 'foo';
}
else {
$this->comment = 'Neither foo, nor a bar'er be.';
}
}
function output()
{
echo $this->comment;
}
}
?>
The basics of the class are the class name (FooBar) and the methods (input and output). Now let's try to use the class:
<?php
/* some html setup */
echo '<html><head><title>Example</title></head><body>';
/* some include or require statement to include the class, this is an example */
require_once 'foobar.class';
/* spawn a new class session (creates an object): */
$text = new FooBar;
/* use the input method */
$text->input('foo');
/* use the output method */
$text->output();
/* We'll do another example just for fun down here... */
/* another hunk of html to break our stuff apart */
echo '<p> </p>';
$another_one = new FooBar();
$another_one->input('Jump Around');
$another_one->output();
echo '</body></html>';
exit;
?>
That's the extreme basics. Useles right? Well, this example is. OOP is however not. Let's take a look at a class example that I have used myself.
<?php
class Table
{
/* Inital setup for the table. */
function tableSetup($rows, $columns)
{
$this->rows = $rows;
$this->columns = $columns;
}
/* Build the table */
function makeTable()
{
echo '<table with="100%" border="1">';
/* loop through the row value to build rows */
for ($r=1; $r<=$this->rows; $r++) {
echo '<tr>';
/* inside each row, loop through the columns */
for ($c=1; $c<=$this->columns; $c++) {
echo '<td>'.$r.'/'.$c.'</td>'; // puts the row and column into the cell
}
echo '</tr>';
}
echo '</table>';
}
}
?>
Now, this class will build a table for us and fill each cell with the the values of $r and $c. Cell 1 in the first row would have the contents of "1/1". The 3rd cell of the 2nd row would have "3/2" as it's contents. Let's try it out:
<?php
/* some html setup */
echo '<html><head><title>Table Example</title></head><body>';
require_once 'table.class';
$table = new Table();
$table->tableSetup(3,4);
$table->makeTable();
echo '</body></html>';
exit;
?>
So... it's: $var = new Classname, $var->method($value); and your off! This is a pretty basic example but should give you some insight on how classes work. If needed a second of this OOP series will be published to talk about extending classes beyond this type of basic functionality. Until then, Have phun...
