PHP variable function args array

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

In trying to make a crude ad hoc template system, I've done something like this:

<?php
if ($page=\"default\") {
 
$template_header = \"header\";
 
$template_header_args = array(\"logo\",\"navigation\",\"searchbox\");
} elseif (
$page=\"404\") {
 
$template_header = \"not_found\";
 
$template_header_args = array(\"message_404\",\"navigation\",\"searchbox\");
}

// later, in the batcave...
$foo = new Foo();
$foo->$template_header($template_header_args);
?>

That's an artificial example. The code I really have is somewhat unwieldy, so I won't post it.

Do you see what I'm trying to do? I want to define properties of a section of the page initially, selecting appropriate content, and have the code render whatever's defined. The variable function itself works; what I don't get is how to pass args on to it -- can't seem to find anything in the manual.

Essentially, I want to pass an array of values on to a function, with each element being a single parameter in the list. Is this possible?

Do args have to be an array? Or comma-delimited string? Should I use call_user_func_array()? If so, how, given the class $foo?

I've experimented for an hour already with no good results. Any ideas?

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

I'm now using pretty much what I posted above, only I've set $foo->header() or $foo->not_found() to take only one parameter:

<?php
Class foo {
  function
header ($args)
  {
    
$something = $args[0];
    
$nextthing = $args[1];
   }
}
?>

I don't know if it's a makeshift solution or that proper way to do it. It works for now, but I'm open to suggestions...

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

You have:

if ($page="default")
'
and
} elseif ($page="404")
'

correct me if I'm wrong but isn't it ment to be "==" ?

Busy's picture

He has: 6,151 posts

Joined: May 2001

I know you only posted part of the code but if your class only has one function/condition it's not worth putting it inside a class.
Also if the navigation and search will be on all pages you can just use a function

function foo($foofoo)
{
//get whatever $foofoo is, like:
echo template header value
display $foofoo
echo template
}

if ($page=="default") {
foo($default);
}elseif($page=="404"){
foo($error);
}

depending how big it's going to get you can define a lot of the $foofoo values.
If the navigation and search wont always be on all pages you can call foo() with 3 values, foo($foofoo,1,0) 1 and 0 being true and false and the actual function works out the rest.

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

The class is huge, huge. Seven functions and growing. Smiling

Also, the navigation and search box is just an example; I have more detailed, unique bits.* It won't be on all pages. There are about five different cases I have so far, and they do change often.

Renegade, that is sample code, and I have made a typing mistake as I invented it. The real code "works" -- no silly errors like that, no need for debugging. This is more of a theory question in that sense.

* I'm avoiding explaining the real scenario because it's not a generic web site. Instead, it's an intranet web application. The bits I have that change are like widgets, and there are many of them, changing based on the application view selected. They follow a similar structure, though, like a header/title and utility/tool bar at the top, message box, content area, footer and diagnostics. It's only the content of these regions that changes (sometimes NULL) -- the data comes from the functions in the class.

Clear as mud? Smiling

Busy's picture

He has: 6,151 posts

Joined: May 2001

clear as mud on a sunny day with a westerly gale blowing.

use a switch statement in the function class to control it all

hard when you don't have the full story but I know what its like when just wanting a small bit of a large picture.

several longer ways to do it also, define $foo's
define foo1 {404,search, nav,footer}
define foo2 {500,nav}
define foo3 {contact,search, help,dirty pic, toast}
define foo4 {none of the above}
....
new foo(foo2)

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.