Jump to content

PHP Class Function Parameters - Any Number of Parameters, call_user_func


Demonic

Recommended Posts

Okay, heres the thing, I'm building my semi cms, its used for a majority of my projects just to get me started with a basic of everything I need.  Backend wise and what not.  It's built off of codeigniter and I'm trying to manipulate a feature that they have in my system.

 

I've created a semi module system which acts just like the controller system codeigniter has, but what I don't have is the extra parameters being passed to my class function.

 

					$action = '_default';
					//show default action
					if( method_exists($class,$action))
					{	
						if( ($this->perms->exists("main_" . $class_name . $action) && $this->perms->has("main_" . $class_name . $action)) OR !$this->perms->exists("main_" . $class_name . $action))
						{
							$url = $this->uri->segment_array();
							return call_user_func(array($class,$action),'test');
						}
						else
						{
							//user has no permissions
							return $this->error("You have no permissions");
						}
					}
					else
					{
						//return error saying default module does not exists
						return $this->error("Default Action does not exists!");
					}

 

Heres a snipet from my code.

 

Above you see $url, that grabs all the urls,

 

Basically I want to pass the results of the urls to the current class function withing using an array, you see how I have "test", thats exactly how I want the parameters to output not: Array()..

 

How can I pass the array (with a foreach) statement to that call_user_func, without "eval"?

 

							$params = "'" . implode("','",$url) . "'";
							eval("call_user_func(array(\$class,\$action),{$params});");

 

Is the only way I can see it being done.

Link to comment
Share on other sites

You mean like this -

call_user_func(array($class, $action), $url);

?

 

Then in call_user_func(), you can loop through $url for all the params.

 

Not really.

 

$url outputs an array, and if I passed $url, I would have to treat the parameter like an array.

 

Array

(

    [1] => admin

    [2] => module

    [3] => folio

    [4] => test

)

 

Heres my default action:

 

public function _default($test)
{
	echo $test;
	$this->template->load( array(
		array(
			"admin/modules/folio/manage",
			array('results' => $this->instance->folio_model->get_array()),
		),
	),"admin");
}

 

If I don't have any actions created for the parameter I provided, it will be come a function parameter.

 

http:// localhost/portfolio/admin/module/folio/test

 

If the function "test" isn't created in the url, that will become the first parameter in the default function

 

_default("test")

 

If there are more url segments it will automatically become the next parameter in the function:

 

http://localhost/portfolio/admin/module/folio/test/test2

 

_default("test","test2")

 

thats only if I haven't created a function named "test".

 

If I created a function named test:

 

it'll look like:

 

test("test")

 

What I'm trying to figure out is if theres an alternative to "eval" to executing that function with the parameters.  Heres what I got so far:

 

/**
 * Plugin Modules
 *
 * This function is used to load built in and user created
 * modules.  Plug and play system for easibility.
 *
 * @param	string	module name
 * @param	string	module sub page
 * @return	void
 */
public function module($module=null,$action=null)
{
	switch( $module )
	{
		case "groups":
			if( !$this->perms->has("main_admin_groups"))
				return redirect("admin/no_permissions");
			return $this->_module_groups();
		break;
		case "manage":
			if( !$this->perms->has("main_admin_modules"))
				return redirect("admin/no_permissions");
			return $this->_module_manage();
		break;
	}

	$perms =& $this->perms;

	//check if module exists and try loading it
	$class_name = "module_" . $module;
	if( $this->module_model->exists($class_name) )
	{
		$file = str_replace("\\","/",APPPATH) . "modules/module_" . $module . EXT;
		if( file_exists($file))
		{
			require_once $file;
			if( class_exists($class_name))
			{
				$class = new $class_name;
				if( !is_null($action) && method_exists($class,$action) && !preg_match("/^_/i",$action))
				{
					//does this action require permissions and does user have them if they do?
					if( ($this->perms->exists("main_" . $class_name . "_" . $action) && $this->perms->has("main_" . $class_name . "_" . $action)) OR !$this->perms->exists("main_" . $class_name . "_" . $action))
					{
						$url = $this->uri->segment_array();
						$rurl = array();
						for($x=5;$x<sizeof($url)+1;$x++)
							$rurl[] = $url[$x];

						if( sizeof($rurl) > 0 )
						{
							$params = "'" . implode("','",$rurl) . "'";
							return eval("call_user_func(array(\$class,\$action),{$params});");
						}
						else
						{
							return call_user_func(array($class,$action));
						}
						//return call_user_func(array($class,$action));
					}
					else
					{
						//user has no permissions
						return $this->error("You have no permissions");
					}
				}
				else
				{
					$action = $this->default_action;
					//show default action
					if( method_exists($class,$action))
					{	
						if( ($this->perms->exists("main_" . $class_name . $action) && $this->perms->has("main_" . $class_name . $action)) OR !$this->perms->exists("main_" . $class_name . $action))
						{
							$url = $this->uri->segment_array();
							$rurl = array();
							for($x=4;$x<sizeof($url)+1;$x++)
								$rurl[] = $url[$x];

							if( sizeof($rurl) > 0 )
							{
								$params = "'" . implode("','",$rurl) . "'";
								return eval("call_user_func(array(\$class,\$action),{$params});");
							}
							else
							{
								return call_user_func(array($class,$action));
							}
						}
						else
						{
							//user has no permissions
							return $this->error("You have no permissions");
						}
					}
					else
					{
						//return error saying default module does not exists
						return $this->error("Default Action does not exists!");
					}
				}
			}
			else
			{
				//warn user module is missing
				return $this->error("The module class is missing!");
			}
		}
	}
	else
	{
		return $this->error("Module does not exists!");
	}
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.