User Name
Password
AppleNova Forums » Programmer's Nook »

PHP: is this a magic function?


Register Members List Calendar Search FAQ Posting Guidelines
PHP: is this a magic function?
Thread Tools
drewprops
Space Pirate
 
Join Date: May 2004
Location: Atlanta
 
2006-03-13, 22:08

I'm looking at the code for a WordPress plugin called "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"....

[php]
$podPressContent .= __('Standard Podcast', 'podpress').': ';
[/php]

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.

Steve Jobs ate my cat's watermelon.
Captain Drew on Twitter
  quote
rollercoaster375
Senior Member
 
Join Date: Mar 2005
Location: UIllinois (Champaign, IL)
Send a message via AIM to rollercoaster375 Send a message via MSN to rollercoaster375 Send a message via Yahoo to rollercoaster375 Send a message via Skype™ to rollercoaster375 
2006-03-13, 22:29

Look for a function named "__" - It must be there somewhere.
  quote
Brad
Selfish Heathen
 
Join Date: May 2004
Location: Zone of Pain
 
2006-03-13, 23:07

It's part of WordPress.

See file wp-l10n.php:

[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;
}
[/php]
  quote
torifile
Less than Stellar Member
 
Join Date: May 2004
Location: Durham, NC
Send a message via AIM to 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...
  quote
Brad
Selfish Heathen
 
Join Date: May 2004
Location: Zone of Pain
 
2006-03-14, 00:07

Programmers!

Back in high school I gave functions proper names, but they were names in Spanish and, you guessed it, Klingon. 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.

The quality of this board depends on the quality of the posts. The only way to guarantee thoughtful, informative discussion is to write thoughtful, informative posts. AppleNova is not a real-time chat forum. You have time to compose messages and edit them before and after posting.
  quote
Majost
monkey with a tiny cymbal
 
Join Date: Nov 2004
Location: Lost
 
2006-03-14, 01:39

Quote:
Originally Posted by torifile
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/

Last edited by Majost : 2006-03-14 at 02:06.
  quote
Brad
Selfish Heathen
 
Join Date: May 2004
Location: Zone of Pain
 
2006-03-14, 01:52

Quote:
Originally Posted by Majost
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:

[php]// 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;
}[/php]
It simply echos the text instead of returning it as a string object.

Quote:
Originally Posted by Majost
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.

The quality of this board depends on the quality of the posts. The only way to guarantee thoughtful, informative discussion is to write thoughtful, informative posts. AppleNova is not a real-time chat forum. You have time to compose messages and edit them before and after posting.
  quote
rollercoaster375
Senior Member
 
Join Date: Mar 2005
Location: UIllinois (Champaign, IL)
Send a message via AIM to rollercoaster375 Send a message via MSN to rollercoaster375 Send a message via Yahoo to rollercoaster375 Send a message via Skype™ to rollercoaster375 
2006-03-14, 09:18

Quote:
Originally Posted by torifile
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
  quote
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.
  quote
drewprops
Space Pirate
 
Join Date: May 2004
Location: Atlanta
 
2006-03-22, 07:06

Checkit!
The dude's first post is in the Programmer's Nook! Kewl.
  quote
ast3r3x
25 chars of wasted space.
 
Join Date: May 2004
Send a message via AIM to ast3r3x  
2006-03-22, 09:36

Quote:
Originally Posted by Brad
Back in high school I gave functions proper names, but they were names in Spanish and, you guessed it, Klingon. 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…
Code:
function() { //thing }
instead of

Code:
function() { //thing }
I HATE that second one.

Last edited by ast3r3x : 2006-03-22 at 09:43.
  quote
Posting Rules Navigation
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Post Reply

Forum Jump
Thread Tools
Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP & Apache 2 ast3r3x Genius Bar 0 2005-03-16 20:38
Search function Banana Feedback 12 2005-02-25 19:49
Generating XML for Newsfeed (MovableType and PHP) drewprops Genius Bar 3 2005-01-22 00:32
HELP! URGENT. DOWNGRADE PHP 5.0.1 to 4.3.6 hype.it Genius Bar 2 2004-08-31 19:15
PHP Catastrophe! ast3r3x Genius Bar 5 2004-08-20 09:10


« Previous Thread | Next Thread »

All times are GMT -5. The time now is 10:44.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004 - 2024, AppleNova