/    Sign up×
Community /Pin to ProfileBookmark

Best practice for getting a result from a function many times?

I have searched on this and I only found one reference to the topic anywhere and it was brief and vague but at least one person said repeating the function over and over is best. But, that doesn’t seem right.

Example:
Using WordPress, I’m writing my functions.php file and I have to call get_template_directory_uri() to get the path to my template files.

Usually developers seem to just use that function over and over as many times as needed. But, it was my understanding that this is wasteful and it’s simpler to just call the function once, put it in a global variable, and use the variable throughout the page.

Or, if globals are undesirable it could at least be called only once per function, assigned to a variable, and then the variable gets used 10 times in a row, to enqueue styles and scripts for example.

So, instead of:

[CODE]… get_template_directory_uri() . ‘/path/to/file/1.css’
… get_template_directory_uri() . ‘/path/to/file/2.css’
… get_template_directory_uri() . ‘/path/to/file/3.css’[/CODE]

It would be:

[CODE]$template_directory_uri = get_template_directory_uri();
… $template_directory_uri . ‘/path/to/file/1.css’
… $template_directory_uri . ‘/path/to/file/2.css’
… $template_directory_uri . ‘/path/to/file/3.css’
[/CODE]

Doesn’t repeating a variable over and over make more sense that using a function repeatedly? It’s not like it’s changing.

What’s the best practice? Thanks

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 25.2015 — Within the same scope, I'd say go ahead and save it to a variable if you need to use it multiple times within that scope. However, I'd avoid using "global" within any functions. If you find yourself needing to do that, then either you need to be able to pass that value as an additional parameter to that function that needs it, or else perhaps create some sort of config class in which you set a static property with that value, e.g.:
[code=php]
class Config
{
public static template_directory_uri;
}

Config::$static_template_directory_uri = get_static_template_directory_uri();

function sample()
{
echo "URI is " . Config::$static_template_directory_uri;
}
[/code]


That being said, you still have a lot of the downside of using "global" in that case, as the dependency on that Config class is hidden within the function that needs it, so I'd still prefer explicitly passing anything that's needed into the function that uses it.
×

Success!

Help @Awesome0 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...