PDA

View Full Version : PHP: is this a magic function?


drewprops
2006-03-13, 22:08
I'm looking at the code for a WordPress plugin called "PodPress (http://www.mightyseek.com/podpress)" by Dan Kuykendall, trying to track down the places where he should add CSS tags so that people can style text created by the plugin. Some of the code he uses is something I haven't yet learned in PHP and I'm wondering if somebody could explain to me what it does.... the closest thing I can find is something called "magic functions"....


$podPressContent .= __('Standard Podcast', 'podpress').': ';


I see that he's concatenating everything on the right side of the ".=" assignment operator into the variable $podPressContent but I don't understand what's happening with the __('','') bit.

I'd like to buy a clue Alex.

rollercoaster375
2006-03-13, 22:29
Look for a function named "__" - It must be there somewhere.

Brad
2006-03-13, 23:07
It's part of WordPress.

See file wp-l10n.php:

// Return a translated string.
function __($text, $domain = 'default') {
global $l10n;

if (isset($l10n[$domain]))
return apply_filters('gettext', $l10n[$domain]->translate($text), $text);
else
return $text;
}

torifile
2006-03-14, 00:02
How's that for an informatively named function?!? I think the wp people know what they're doing, so there must be a reason they named it __. I think...

Brad
2006-03-14, 00:07
:lol: Programmers!

Back in high school I gave functions proper names, but they were names in Spanish and, you guessed it, Klingon. :D Since my programs generally worked perfectly and implemented lots of extra features that weren't even requested, my teacher didn't mind not being able to follow the code.

Seriously, some programmers are just notorious for giving things awful names like that. There are actually code obfuscation contests that hinge on this idea. The problem is rather epidemic, apparently, because even in my 2nd senior year in computer science, professors are still harping about following proper naming conventions.

Majost
2006-03-14, 01:39
How's that for an informatively named function?!? I think the wp people know what they're doing, so there must be a reason they named it __. I think...
I think it's just that they use it all over the place. And IIRC _e() is their echo error. Or something like it. I think they just wanted something short, so they didn't have to type translate() every time they wanted to output something.

But, yeah, it took me forever to figure it out, too. I wish there was a nice linear guide to wordpress... I can never dig my way through the codex or function lookups without getting sidetracked 10 different ways. It takes me forever to figure out most functions.

Oh, and Brad... the daily wtf is a marvelous site for that kind of code obfuscation. Let's see... Ah, here it is: http://thedailywtf.com/

Brad
2006-03-14, 01:52
I think it's just that they use it all over the place. And IIRC _e() is their echo error. Close, but not quite. It's right after function __ in the aforementioned file:

// Echo a translated string.
function _e($text, $domain = 'default') {
global $l10n;

if (isset($l10n[$domain]))
echo apply_filters('gettext', $l10n[$domain]->translate($text), $text);
else
echo $text;
}
It simply echos the text instead of returning it as a string object.

I think they just wanted something short, so they didn't have to type translate() every time they wanted to output something.
Still, it's an elementary programming technique that really isn't excusable. It's akin to naming objects like "a", "b", "c1", "c2", or "df" just... because. This practice benefits essentially no one but the initial programmer and his own ego. If goes without saying that it seriously hampers extensibility.

No, that's not years' of professors drilling that into my head. :) It's common sense that I quickly figured out after writing those programs in Spanish and Klingon and looking at them again mere weeks later.

rollercoaster375
2006-03-14, 09:18
How's that for an informatively named function?!? I think the wp people know what they're doing, so there must be a reason they named it __. I think...
Actually, in my experience, the code behind WordPress is some of the worst I've ever seen :p

rob.daemon
2006-03-22, 02:13
If I remember correctly, this is a carry-over from the C gettext library. It has an implementation in PHP: http://us2.php.net/manual/en/function.gettext.php

And if you note, it can be referenced through an _() function call.

I'd imagine this is their version of it. The reason why it's poorly named is to make it not add intrusive amounts of code to support localizations.

drewprops
2006-03-22, 07:06
Checkit!
The dude's first post is in the Programmer's Nook! Kewl.

ast3r3x
2006-03-22, 09:36
Back in high school I gave functions proper names, but they were names in Spanish and, you guessed it, Klingon. :D Since my programs generally worked perfectly and implemented lots of extra features that weren't even requested, my teacher didn't mind not being able to follow the code.
Heh, must have been nice, if my C++ teacher didn't couldn't follow, or you used a function that wasn't standard, he would just laugh and hand it back to you. Not meanly, but he literally thought it was funny that you hoped he would accept it.

Basic programming classes are so sweet because they are easy, and something might take you a day that takes others a week. That was an AWESOME class.

I never added extra features though, my goal was to do it in as few lines of code as possible without compromising my method of formatting…
function()
{
//thing
}

instead of

function() {
//thing
}

I HATE that second one.