Hi everyone,

Got two mind-puzzling questions for you tha I have been pondering over for the best part of a day now.

Q1: Is it possible to have a PHP variable in the name of a function, as in

Function $variable () {
function code here
}

or something alike.

Q2 How do you do it if it is indeed possible ;)

All help much appreciated!

Jack

Recommended Answers

All 9 Replies

Uhh... It's not possible but if you use a class you can do something similar. Like this

$myFunction = new classFunctions();

Hi britoniah3480, i kinda need something such as a function that has an ID in it, for example I have many posts in a thread, generated on the fly from sql via the use of php in a while loop. I need a function to be created for each post or another way of the function using the UNIQUE ID of the post, as each post is individual and has its own ID.

I hope that makes sense... makes sense in my head xD lol

Wouldn't it be easier to pass your id as a parameter to the function ?

function getPost($postId) { }

Try learning how to use Objects in PHP

Then declare an Array with it for example:

$Arr[3433] = new objClass();
$Arr[3433]->getFirstname();

pritaeas is right on this one. I think you need to better explain what you are trying to do.
you simply need to pass each id into a function you do not need a dynamic 'function' generator to handle your data.

My aim is to have a function created in a while loop. In the while loop posts are retrieved from a db via sql . Each post has an id and what i wanted to do was have the post id used as the function name because simply placing a function in a while loop fails because it can only be declared once. So i need some way of being able to use a function once but that is generated on the fly. Each post has its own variables in which i need to use in the function, so if there is some way i can do that then that would be great!

Cheers.

How many different scenarios do you have ? Can you give a pseudo code example of what you'd want ?

<?php
define(DB_HOST,'db.server');
define(DB_USER,'username');
define(DB_PASS,'password');
define(DB_BASE,'name_of_database');

// **********************
// CLASS DECLARATION
// **********************
// class : begin  // This is a class that I already have and am altering it for you I'm not removing 
/************************   GO TO LINE 248 and look at the function select_by_id($id)  ***********************/
class example { 
// **********************
// ATTRIBUTE DECLARATION
// **********************
	var $announcement_id;   // KEY ATTR. WITH AUTOINCREMENT
	var $category;   // (normal Attribute)
	var $title;   // (normal Attribute)
	var $body;   // (normal Attribute)
	var $signature;   // (normal Attribute)
// **********************
// CONSTRUCTOR METHOD
// **********************
	/**
	* @type	- public
	* @desc	- constructor
	* @param	- celebrate
	* @return	- Instance of $class
	* @vers	- 1
	* @Mod	- 
	**/
	function example() {
		
	}
// **********************
// GETTER METHODS
// **********************
	/**
	* @type	- public
	* @desc	- get variable value
	* @param	- get_announcement_id()
	* @return	- variable
	* @vers	- 1
	* @Mod	- 
	**/
	function get_announcement_id() {
		return $this->announcement_id;
	}

	/**
	* @type	- public
	* @desc	- get variable value
	* @param	- get_category()
	* @return	- variable
	* @vers	- 1
	* @Mod	- 
	**/
	function get_category() {
		return $this->category;
	}

	/**
	* @type	- public
	* @desc	- get variable value
	* @param	- get_title()
	* @return	- variable
	* @vers	- 1
	* @Mod	- 
	**/
	function get_title() {
		return $this->title;
	}

	/**
	* @type	- public
	* @desc	- get variable value
	* @param	- get_body()
	* @return	- variable
	* @vers	- 1
	* @Mod	- 
	**/
	function get_body() {
		return $this->body;
	}

	/**
	* @type	- public
	* @desc	- get variable value
	* @param	- get_signature()
	* @return	- variable
	* @vers	- 1
	* @Mod	- 
	**/
	function get_signature() {
		return $this->signature;
	}

	// **********************
	// SETTER METHODS
	// **********************
	/**
	* @type	- public
	* @desc	- set variable value
	* @param	- set_announcement_id($val)
	* @return	- void
	* @vers	- 1
	* @Mod	- 
	**/
	function set_announcement_id($val) {
		$this->announcement_id = $val;
	}

	/**
	* @type	- public
	* @desc	- set variable value
	* @param	- set_category($val)
	* @return	- void
	* @vers	- 1
	* @Mod	- 
	**/
	function set_category($val) {
		$this->category = $val;
	}

	/**	 
	* @type	- public
	* @desc	- set variable value
	* @param	- set_title($val)
	* @return	- void
	* @vers	- 1
	* @Mod	- 
	**/
	function set_title($val) {
		$this->title = $val;
	}

	/**	 
	* @type	- public
	* @desc	- set variable value
	* @param	- set_body($val)
	* @return	- void
	* @vers	- 1
	* @Mod	- 
	**/
	function set_body($val) {
		$this->body = $val;
	}

	/**
	* @type	- public
	* @desc	- set variable value
	* @param	- set_signature($val)
	* @return	- void
	* @vers	- 1
	* @Mod	- 
	**/
	function set_signature($val) {
		$this->signature = $val;
	}
	// **********************
	// INSERT
	// **********************
	/**
	 
	* @type	- public
	* @desc	- insert this object
	* @param	- insert
	* @return	- boolean
	* @vers	- 1
	* @Mod	- 
	**/
	function insert() {
		$this->announcement_id = ""; // clear key for autoincrement
		$sql = "INSERT INTO some_database_tablename ( category,title,body,signature,) VALUES ( '".$this->slashes($this->category)."','".$this->slashes($this->title)."','".$this->slashes($this->body)."','".$this->slashes($this->signature)."')";
		$dblink = null;
		try	{
			$dblink = mysql_connect(DB_HOST,DB_USER,DB_PASS);
			mysql_select_db(DB_BASE,$dblink);
		} catch(Exception $ex) {
			echo "Could not connect to " . DB_HOST . ":" . DB_BASE . "\n";
			echo "Error: " . $ex->message;
			exit;
		}
		
		$retid = mysql_query($sql,$dblink) or die(mysql_error());
		if (!$retid) { 
			echo( mysql_error()); 
		}		
		$this->announcement_id = mysql_insert_id($dblink);
		
		if(is_resource($dblink)) {		
			mysql_close($dblink);
		}		
		return $retid;
		
	}

	// **********************
	// UPDATE
	// **********************
	/**
	* @type	- public
	* @desc	- update this object
	* @param	- update($id)
	* @return	- boolean
	* @vers	- 1
	* @Mod	- 
	**/
	function update($id) {
		$sql = " UPDATE some_database_table SET  category = '$this->category',title = '$this->title',body = '$this->body',signature = '$this->signature', WHERE announcement_id = $id ";
		$dblink = null;
		try	{
			$dblink = mysql_connect(DB_HOST,DB_USER,DB_PASS);
			mysql_select_db(DB_BASE,$dblink);
		} catch(Exception $ex) {
			echo "Could not connect to " . DB_HOST . ":" . DB_BASE . "\n";
			echo "Error: " . $ex->message;
			exit;
		}
		
		$retid = mysql_query($sql,$dblink) or die(mysql_error());
		if (!$retid) { 
			echo( mysql_error()); 
		}		
		
		if(is_resource($dblink)) {		
			mysql_close($dblink);
		}				
		return $retid;

	}
	
	/******************************************************************************************/
	/*****  THIS IS THE METHOD YOU WOULD USE IN YOUR WHILE LOOP!!!!!!!!!!!!!!!!!!!!!!!!!  *****/
	/******************************************************************************************/
	// **********************
	// SELECT METHOD / LOAD
	// **********************
	/**
	* @type	- public
	* @desc	- load this object
	* @param	- select_by_id($id)
	* @return	- sets this object from "$id"
	* @vers	- 1
	* @Mod	- 
	**/
	function select_by_id($id) {
		$dblink = null;
		try	{
			$dblink = mysql_connect(DB_HOST,DB_USER,DB_PASS);
			mysql_select_db(DB_BASE,$dblink);
		} catch(Exception $ex) {
			echo "Could not connect to " . DB_HOST . ":" . DB_BASE . "\n";
			echo "Error: " . $ex->message;
			exit;
		}
		$sql =  "SELECT * FROM some_table_name WHERE announcement_id = '".$this->slashes($id)."'";
		
		$retid = mysql_query($sql,$dblink) or die(mysql_error());
		if (!$retid) { 
			echo( mysql_error()); 
		}
		if ($row = mysql_fetch_array($retid)) {	
			$this->announcement_id = $row->announcement_id;
			$this->category = $row->category;
			$this->title = $row->title;
			$this->body = $row->body;
			$this->signature = $row->signature;
		}
		
		if(is_resource($dblink)) {		
			mysql_close($dblink);
		}
		return $this;
	}
	/******************************************************************************************/
	/*****  THIS IS THE METHOD YOU WOULD USE IN YOUR WHILE LOOP!!!!!!!!!!!!!!!!!!!!!!!!!  *****/
	/******************************************************************************************/	
	// **********************
	// DELETE
	// **********************
	/**
	 
	* @type	- public
	* @desc	- delete this object
	* @param	- delete($id)
	* @return	- boolean
	* @vers	- 1
	* @Mod	- 
	**/
	function delete($id) {
		$dblink = null;
		try	{
			$dblink = mysql_connect(DB_HOST,DB_USER,DB_PASS);
			mysql_select_db(DB_BASE,$dblink);
		} catch(Exception $ex) {
			echo "Could not connect to " . DB_HOST . ":" . DB_BASE . "\n";
			echo "Error: " . $ex->message;
			exit;
		}
		$sql = "DELETE FROM some_table_name WHERE announcement_id = $id;";
		
		$retid = mysql_query($sql,$dblink) or die(mysql_error());
		if (!$retid) { 
			echo( mysql_error()); 
		}
		
		if(is_resource($dblink)) {		
			mysql_close($dblink);
		}
		return $retid;
	
	}
/**
	 
	* @type	- public
	* @desc	- $str
	* @param	- adding slashes if necessary
	* @return	- db safe string
	* @vers	- 1
	* @Mod	- 
	**/
	function slashes($str) {
		//if ((get_magic_quotes_gpc()) && (!empty($str))) {
		if (!empty($str)) {
			return addslashes($str);
		} else {
			return $str;
		}
	}
	/**************************************************************/
	/*          ADD YOUR CUSTOM FUNCTIONS BELOW			       */
	/**************************************************************/
	// **********************
	// SELECT METHOD / LOAD
	// **********************
	/**
	* @type	- public
	* @desc	- loads categories into an array to be passed back to directory object.
	* @param	- get_all_categories($id)
	* @return	- sets this object from "$id"
	* @vers	- 1
	* @Mod	- 
	**/
	function get_all_announcements() {
		$dblink = null;
		try	{
			$dblink = mysql_connect(DB_HOST,DB_USER,DB_PASS);
			mysql_select_db(DB_BASE,$dblink);
		} catch(Exception $ex) {
			echo "Could not connect to " . DB_HOST . ":" . DB_BASE . "\n";
			echo "Error: " . $ex->message;
			exit;
		}
		$sql =  "SELECT * FROM some_table_name";		
		$retid = mysql_query($sql,$dblink) or die(mysql_error());
		if (!$retid) { 
			echo( mysql_error()); 
		}
		$ret_array = array();
		//echo "cat sql = " . $sql . "<br>";
		//$listings_array = array();
		while ($row = mysql_fetch_array($retid)) {	
			$c = new example();			
			$c->announcement_id = $row['announcement_id'];
			$c->category = $row['category'];
			$c->title = $row['title'];
			$c->body = $row['body'];
			$c->signature = $row['signature'];
			array_push($ret_array,$c);
		}
		if(is_resource($dblink)) {		
			mysql_close($dblink);
		}
		return $ret_array;
	}		
} // class : end
?>


I am trying to come up with an example of how to use this off of your 'explanation' but it is still unclear as to what you are asking for.
'in the while loop posts are retrieved from a db'  I'm trying to wrap my head around this statement.
A form posts to a page / server.  You query a db for data.  You insert/query a db to put data into a database or get it out.
Maybe I'm not following your terminology.
You can also write a function to take a form post and process it, although I have not included it in this example...
Anyway, you can use this code like this.
at the top of your page include this class.
include('example.class.php');

if you have a bunch of ids?.....

while($i_have_more_ids) {
	// I'm not sure how we got to this point, but you say you have multiple ids to process so for each id...
	$id = $row['id'];  // again, not sure where each id is coming from.
	// declare a new class object.
	$ex = new example();
	$my_object = $ex->select_by_id($id);
	// At this point your $my_object now would hold the following values pulled from the db:
	echo "id = " . $my_object->announcement_id. "<br / >";
	echo "category = " . $my_object->category . "<br />";
	echo "title = " . $my_object->title . "<br />";
	echo "body = " . $my_object->body . "<br />";
	echo "signature = " . $my_object->signature . "<br />";
}

Last thought.  The more I read your question without clarity it really sounds like you want to pull data from a db and do something with it.
If that is the case check the get_all_announcements() function.
It would return you an array of all 'records' on your table used like:
(include the class)
// placeholder
$x = array();
//declare new object
$e = new example();
// fill your array with data
$x = $e->get_all_announcements();
// loop through that data
foreach($x as $my_object) {
	echo "id = " . $my_object->announcement_id. "<br / >";
	echo "category = " . $my_object->category . "<br />";
	echo "title = " . $my_object->title . "<br />";
	echo "body = " . $my_object->body . "<br />";
	echo "signature = " . $my_object->signature . "<br />";
}

Now what exactly are you trying to do again?
Member Avatar for diafol

You can do this:

$fname = "myfunc";

if (function_exists($fname)) {
  $fname();
}

if you have:

function myfunc(){
 ...
}

it will run

You can modify it further with parameters.

//EDIT
However, reading a few other posts, I don't know what the heck you're trying to do. The whole point of functions is that they're reusable. I don't see the point of producing them on the fly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.