NameSilo

Escape A Function...

NamecheapNamecheap
Watch

beaver6813

Established Member
Impact
16
Hey,

Basically there is a very annoying thing when you define an array();, if say i had :

$TheArray = array(get_content(true),$bla,get_header());

It would actually run those functions even though i haven't individually picked them out, is there anyway to define these in an array without it running them? I've tried stuff like enclosing them in '' but then it presumes they're a string and cocks up when i try eval() on them.. :S

Im out of ideas guys! Help!
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Can you supply some code so we can see how you are using this it might help sort a way of fixing the problem, It might be as simple as changing your logic.
 
0
•••
Hey,

Its really just that :P

I put in some debugging code with a test function to see when if the test variable increased after the array was defined (it shouldn't increase) and it did... which means it was running the functions.

I'm just wondering whether is there anyway to define the functions in an array without running them when defining them... like

PHP:
$mrarray = array(get_content(true),$bla,get_header);
//Above... before i want it to the function is being run when defined
foreach($mrarray as $value) {
$return .= $value;
}

I have no idea if i did it correctly, but it works:

$MrArray array('get_content(true),'$bla','get_footer()');
echo eval("echo {$MrArray[$key]}".';');

You get the point... hardly ideal, but i don't know why the array is running the functions when declared :S
 
0
•••
What's wrong with running the functions? Do they echo something?
 
0
•••
tm said:
What's wrong with running the functions? Do they echo something?

Yup... well its not that thats the problem, its just that they run some queries... which don't have to be run ^^
 
0
•••
okay can you do something like the following:-

PHP:
$TheArray = array('get_content',$bla,'get_header');

then when you wish to use the first function in the array you would for example do:-

PHP:
$TheArray[0]();

However doing this is not very intuitive. Unless the contents of $TheArray are dynamic then you should know when you are calling the functions so why not just call them when they are actually needed?
 
0
•••
Its basically for a template system, it searches through the templates for tags, if it finds one it replaces with the appropriate function defined in the array and runs it :)
 
0
•••
Yes but is there something special within the array? Do you have multiple arrays that might be called depending on the situation? If not why not call them normally? If you want reusable code put it within a function.

What I posted in my previous post will work but is not ideal.
 
0
•••
Yeah, I don't see why you want it to be in an array, you're just making it difficult for yourself...
 
0
•••
Well where else am i going to put my list of Tags->Functions?
 
0
•••
Show us the actual script and we will be able to help more. You are effectively giving us a hypothetical question which cannot be answered.

If you show us the actual code we may see why your logic is so and most likely advise a better way of doing it.

Also as it is a template system, most of such scripts use classes so that can be reused properly. The way you seem to be doing it if you wish to change something in the way it works you will have to change it everywhere it is used instead of just 1 place.
 
0
•••
Okay :) Here is the template function, its everything thats being used, the $replace variable when the template_process functions is called is basically the contents of the main index file which will contain functions such as <-(GET_HEADER)->, <-(GET_FOOTER)-> etc :)

Code: http://pastebin.co.uk/11611

EDIT: Sorry for the lack of comments :P And its untidiness ^^
 
0
•••
The problem is, is that you are trying to do too much in 1 function. Sure you can use this code with only 1 line of code in your calling script but it is too specific and not exactly reusable.

A better way to do this would be to pass 2 parameters to the array 1 the string that is being replaced and 2 what is replacing it. Sure it will mean more lines of code in the calling script but much more adaptable for use in your pages. Alternatively why not look at some already existing template scripts such as http://pear.php.net/html_template_it
 
0
•••
Hey,

Okay i'll try and improve the system a bit more :)

Thanks for the advise ;)
 
0
•••
Dynadot — .com TransferDynadot — .com Transfer
CatchedCatched

We're social

Escrow.com
Spaceship
Rexus Domain
CryptoExchange.com
Domain Recover
CatchDoms
DomDB
NameFit
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back