Hour Eight

Creating Objects

Classes are the template (code) from which objects are created. An object is an instance of a class which exists in memory.

<?php
#declare property using var
class my_first_class {
	var $name = "harry";
}//end my first class

#access the property $name outside of of the object
$obj1 = new my_first_class();
$obj2 = new my_first_class();

#now we change the object $name from harry to bob
#by accessing it using a special operator
#$name was born harry we change it by assigning the string bob
#to the name property.
$obj1->name = "bob";
print "$obj1->name
"; print "$obj2->name
"; ?>

Below is the PHP printed out results for the property $name:

bob
harry

Object Methods

A method is a function defined within a class and every object of that class will have that method's functionality.

Listing 8.1 - A class with a method

<?php
class first_class {
	var $name;
	function sayHello() {
		echo "Hello";
		}//end function sayHello
}//end first class

$obj1 = new first_class();
$obj1->sayHello();
#outputs "hello"
?>

Below is the output "Hello" from the method sayHello:

Hello

Listing 8.2 - Accessing a property from within a method

<?php
class second_class {
	var $name="harry";
	function sayHello() {
		echo "hello my name is $this->name<br />";
		}//end function sayHello
	}//end second_class
	
$obj1 = new second_class();
$obj1->sayHello();
#outputs "hello my name is harry"
?>

Below is the output from the second_class method sayHello by having the object refer to itself using the $this variable and the -> operator:

hello my name is harry

Listing 8.3 - Changing the value of a property from within a method

<?php
class third_class {
	var $name = "harry";
	function setName( $n ) {
		$this->name = $n;
		}//end function setName
	function sayHello() {
		echo "hello my name is $this->name<br />";
		}//end function sayHello
}//end third_class

$obj1 = new third_class();
$obj1->setName("william");
$obj1->sayHello();
#outputs "hello my name is william"
?>

Below is the output from the method sayHello. I passed an argument to the method setName. The method setName changed the object's name from harry to william.

hello my name is william

Listing 8.4 - A class with a constructor

<?php
class fourth_class {
	var $name;
	function fourth_class( $n = "anon" ) {
		$this->name = $n;
		}//end fourth_class constructor
	function sayHello() {
		echo "hello my name is $this->name<br />";
		}//end sayHello method
}//end fourth_class

$obj1 = new fourth_class("bob");
$obj2 = new fourth_class("harry");
$obj1->sayHello();
#outputs "hello my name is bob"
$obj2->sayHello();
#outputs "hello my name is harry"
?>

Below is the output where I have used a constructor (function fourth_class) which is automatically called when I instantiate a fourth_class object. If no argument is included when the object is created, the name will be anon.

hello my name is bob
hello my name is harry

8.4 - Same Class, but no argument is passed

$obj1 = new fifth_class("bob");
$obj2 = new fifth_class();
$obj1->sayHello();
#outputs "hello my name is bob"
$obj2->sayHello();
#outputs "hello my name is anon"
hello my name is bob
hello my name is anon

Listing 8.5 - Bringing it all together

The Table Class

Create a class that will maintain a table of fields, organized in named columns.

<?php
class Table {
	var $table_array = array (); //multidimensional array for the rows
	var $headers = array(); //array for column names
	var $cols; //integer to track the number of columns we have

function Table( $headers ) {
	$this->headers = $headers; //constructor uses an array of strings as a parameter
	$this->cols = count( $headers ); //calculate the number of columns and assign to cols property
	}//end Table constructor

# the addRow90 method expects an array in a parameter variable called $row
# the number of columns expected are stored in $cols
# array_push is used to add the row to the table_array. Two arguments are passed the array to add
# on to, followed by the value to be pushed on.

function addRow( $row ) {
//using the count() function to check that $row contains the right number of elements
	if(count($row) != $this->cols) 
		return false; //if $cols does not equal the number in the array $row
	array_push($this->table_array, $row);
	return true;
	} //end method addRow

# addrow() is fine if the elements in the array are ordered correctly. 
# addRowAssocArray is more flexible because it will ignore any key that does not match a header

function addRowAssocArray( $row_assoc ) {
	$row = array(); //create and empty array to store values that will be added to table_array
	
# loop thru the $headers array to check that a value corresponding to each string exists in the
# $row_array. It returns ture or false.

	foreach ($this->headers as $header) {
		if(!isset($row_assoc[$header]))
		
# if no element indexed by that string exists in $row_array, 
# create one with a value of an empty string.

			$row_assoc[$header] = ""; 
			
# build up the $row array by adding the element in $row_assoc indexed by the the current string
# in the headers array. $row now contains an ordered copy of the values passed in $row_assoc,
# with empty strings in place of omissions.

			$row[] = $row_assoc[$header];
			}//end foreach
		array_push($this->table_array, $row);
		return true;
	}//end addRowAssocArray method

# Output() writes the headers and the table_array properties to the browser.
# table_array is a two dimensional array so each of its elements are an array that
# needs to be looped thru.

function output() {
	echo "<pre>";
	foreach($this->headers as $header) //loop thru and print headers
		echo "<b>$header</b>  ";
	echo "\n";
	
# main loop for table_array

	foreach($this->table_array as $y) {
	
# secondary loop for array elements of table_array

		foreach($y as $xcell)
			echo "$xcell  ";  //print the table_array array information
		echo "\n";
		} //end foreach
	echo "</pre>";
	}//end method output
}//end class Table

$test = new table(array("a", "b", "c",));
$test->addRow(array(1,2,3));
$test->addRow(array(4,5,6));
$test->addRowAssocArray(array(b=>0, a=>6, c=>3));
$test->output();
?>

Below is the output from the Table class created.

a  b  c  
1  2  3  
4  5  6  
6  0  3