Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on 

Is this an example of a fluent interface?

I have a Class that has within it two methods. The first method grabs an ID that's arriving from a form and, via a select statement, retrieves the name of the table that corresponds to that ID.

The next method's job is to retrieve the column names of the table the previous method has ascertained.

It looks like this:

class TableData {

	public $table_name;

	function TableName()
	{
		global $mysqli;

		$sql="select table_name from tools where id='$_POST[table_id]'";
			if(!$query=$mysqli->query($sql))
			{
				$err=$mysqli->errno.': '.$mysqli->error;
				trigger_error($err, E_USER_WARNING);
			}
		$row=$query->fetch_object();
		$the_name=$row->tool_name;
		
		$this->table_name=$the_name;
	}

	function TableInfo()
	{
		global $mysqli;
		
		$sql = "show columns from $table_name";
		$query = $mysqli->query($sql);
		
		if(!$query)
		{
		$error = $mysqli->errno.': '.$mysqli->error;
		trigger_error($error, E_USER_WARNING);
		}
		
		$data_count=mysqli_num_rows($query);
		
		if($data_count==0)
		{
		trigger_error("you don't have any column names", E_USER_WARNING);
		}
		//return $data_count;
		
		while($show_columns=$query->fetch_object())
		{
		$the_columns[]=$show_columns->Field;
		}
		return $the_columns;
	}
}

Open in new window


I imagine you could retrieve the name of the table and then pass it on as a variable into a subsequent function, but I'm thinking there's a more elegant way to do this. As I was looking for some answers I stumbled upon the term "fluent interface" and I'm not sure if that's what I'm looking for, but there it is.

Can I set in motion a "daisy chain" of methods by instantiating one Class? If so, how. If not, what are my options in terms of sound programming?
PHP

Avatar of undefined
Last Comment
Ray Paseur
SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Bruce Gust
Bruce Gust
Flag of United States of America image

ASKER

Kaufmed - thanks for getting back with me!

I had already seen the example you referenced on the Wiki page. Thing is, I couldn't figure out how to do it in the context of my scenario.

Going back to that for a moment, I was able to get it to work. Here's my Class:

class TableData {

	public $table_name;

	function TableName()
	{
		global $mysqli;

		$sql="select table_name from tools where id='$_POST[table_id]'";
		if(!$query=$mysqli->query($sql))
			{
				$err=$mysqli->errno.': '.$mysqli->error;
				trigger_error($err, E_USER_WARNING);
			}
		$row=$query->fetch_object();
		$the_name=$row->table_name;
		
		return $the_name;
	}

	function TableInfo($table_name)
	{
		global $mysqli;
		
		$sql = "show columns from $table_name";
		echo $sql;
		$query = $mysqli->query($sql);
		
		if(!$query)
		{
		$error = $mysqli->errno.': '.$mysqli->error;
		trigger_error($error, E_USER_WARNING);
		}
		
		$data_count=mysqli_num_rows($query);
		
		if($data_count==0)
		{
		trigger_error("you don't have any column names", E_USER_WARNING);
		}
		//return $data_count;
		
		while($show_columns=$query->fetch_object())
		{
		$the_columns[]=$show_columns->Field;
		}
		return $the_columns;
	}
}

Open in new window


And then here's what I instantiated things so I could get my needed result:

$table_stuff=new TableData;
$table_handle=$table_stuff->TableName();
$columns = $table_stuff->TableInfo($table_handle);

It works! But if I were looking to something a little more elegant / streamlined and I wanted one call, one Class and one result, how would that look? Can that be done?
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of kaufmed
kaufmed
Flag of United States of America image

Notice that the first call, FetchTableName, returns the object itself, not any data. Only the last call in the chain returns something other than the object itself.
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo