1

I am currently developing an advertising module for a custom CMS, and using template tags to allow customers to add adverts into their pages through a WSYWIG page content editor.

Eg. {=advert_1}

On the frontend, this will be found through a regex and then be converted into a function, which will look to a database to select and display an advert

Template_tags.php

while ($advertRow = $advertResult->fetch_assoc()) {
    $advertGroupID = $advertRow['grpID'];
    $advert = "advert_";

    ${$advert . $advertGroupID} = showAdvert($advertGroupID);
}

This means {=advert_1} will be converted to showAdvert(1)

The problem I am having is that the showAdvert function will be run for all adverts regardless of whether or not it appears on the page, which then adds to the "views", even though the advert may not be displayed.

What I want is to just define the function without executing it, so when it appears in the page content, only then will the function will be executed.

4
  • 1
    I have no idea what you mean. If {=advert_1} is on the page then it will be converted to showAdvert(1) and run. If not, it won't be converted and won't run. What's the prob? Commented May 23, 2016 at 15:43
  • Any time you use variable variables, you should really be using an array. Commented May 23, 2016 at 15:44
  • Because it's grabbing the grpID from the database, it is looping through these and creating a function for every "group", and executing each one on the line ${$advert . $advertGroupID} = showAdvert($advertGroupID); Commented May 23, 2016 at 15:46
  • This is my first time using variable variables, and still find them strange. What would the array alternative be? Commented May 23, 2016 at 15:48

1 Answer 1

1

Use a function expression to create a closure.

${$advert . $advertGroupID} = function() use($advertGroupID) {
    showAdvert($advertGroupID);
};

To call the function, you need to put parentheses after it:

$name = 'advert_1';
echo $$name();

To use it with preg_replace_callback

preg_replace_callback("/\{=([^\{]{1,100}?)\}/", function($match) {
    return $match[1]();
}, $pageContent);
Sign up to request clarification or add additional context in comments.

9 Comments

I got an unexpected ")" on the last line, and when I took that out I got "Catchable fatal error: Object of class Closure could not be converted to string". This is was on this line $pageContent = preg_replace("/\{=([^\{]{1,100}?)\}/e", "$$1", $pageContent);
Did you get that error when you called the function? Sounds like you forgot to put () after the variable containing the function, so you were trying to echo the function itself instead of calling it.
I think it should be "$$1()" so it calls the function. BTW, you shouldn't use the e modifier in preg_replace, you should use preg_replace_callback.
I am echoing the function, because I'm echoing out the $pageContent which has the template tag {=advert_1} in it. The showAdvert() function then returns the html for the advert.
But you don't want to echo the function, you want to call the function at that point.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.