Adding Control Functions

From XOMBO Documentation

Jump to: navigation, search

Introduction

This tutorial assumes you are using views and controls based on previous tutorial examples.

This guide will give you a brief overview of how you might manipulate batches of objects with your own custom functions.

Instance Functions

Since all pets play, we're going to add a function called “play” to our Control DBObject “pet”.

	function play () { 
		// do some playful things 
		return ($this->PetName . “ the “ . $this->BreedName . 
		“  is playing\n”); 
	} 

In a View Action, to play with our instance of pet we can simply do this:

	$Pet = new Pet (1); 
	echo $Pet->play (); 

Static Functions

We're going to need a function called “playAll” in our Control DBObject "Pet" that will execute the play function for all pets.

	static function playAll () { 
		$Pets = Pet::populate (); // get all pets 
		$return = ''; 
		// iterate through all instances of Pet in $Pets
		if (is_array ($Pets) && count ($Pets)) foreach ($Pets as $Pet) {
			// play with the current pet
			$return .= $Pet->play ();
		} 
		return $return; 
	} 

From within a View Action, to play with all our pets we can simply do this:

	echo Pet::playAll ();