[wp-hackers] echo and return
David House
dave at xmouse.ithium.net
Mon Jul 26 12:17:30 UTC 2004
This is a very, very good idea and I think it should be prioritised. It would
make WP a _lot_ easier to work with, as well as making more sense.
By the way, there's an easy way to get round the mass-number-of-functions
problem. Simply define all the get_the_* functions, then:
$funcs = get_defined_functions();
$funcs = $funcs['user'];
foreach ($funcs as $funcname)
if (substr($funcname, 0, 4) == 'get_') {
$newfname = substr($funcname, 4); //4 is the length of string 'get_'
if (!function_exists($newfname)) {
function $newfname($arrs) {
{$funcname}($arrs);
}
}
BUT that's only if every single function was converted over to url style
arguments, which wouldn't be an ideal situation. It could also work if there
was a PHP function to grab the arguments of any function, which there doesn't
seem to be.
Quoting Brian Meidell <brian at mindflow.dk>:
> Hi hackers,
>
> I had a short and confusing discussion with Mookitty and Matt on the irc
> channel today, and I wanted to pick it up here.
>
> I suggested to Matt that I would like to split up template functions
> that right now either:
> 1) Echo their output, or
> 2) Have a parameter that decides whether to echo or return output
>
> So instead of having
> the_foo($id, $pre = '', $post = '', $foo = '', $echo = true)
> {
> if($echo)
> echo "$pre test $post";
> else
> return "$pre test $post";
> }
>
> we would have:
>
> get_the_foo($id, $pre = '', $post = '', $foo = '')
> {
> return "$pre test $post";
> }
>
> and:
>
> the_foo($id, $pre = '', $post = '', $foo = '', $echo= true)
> {
> if($echo)
> echo get_the_foo($id, $pre, $post, $foo);
> else
> return get_the_foo($id, $pre, $post, $foo);
> }
>
> ... everywhere we could.
> There are functions that follow this pattern already, but I would like
> to make changes changes, so they all do (as much as possible, at least).
>
> My arguments for this are:
>
> 1) Often the_foo doesn't have the $output flag, and the only way to get
> the data is either by catching the output with the ob_ functions - which
> is extremely ugly - or copying the body of the function into your own
> function, which is unfortunate and can prevent smooth version migration.
>
> 2) Even when the function has the $echo, it's sometimes the last
> argument (as shown in the_foo), and you have to enter a bunch of
> arguments where the defaults were doing just fine, which is a pain.
>
> 3) It's a bad programming practice to have an unnecessary branch
> runtime, if the branch path is known at design time.
>
> 4) Even when the $echo argument is cleverly put first, it's cluttering
> the argument list for anyone that doesn't need it. It also causes
> confusion with non-technical minded people. My friend who runs binary
> bonsai came in while I was writing this email, and didn't know how to
> use get_the_category_link because he didn't know the meaning of the echo
> argument.
>
> Mookitty would prefer that all functions just return their data, and
> that people use:
> <?=the_foo() ?>
>
> but Matt argued that we don't use the <? shorthand because of
> compatability, and it would be too difficult for the template users to
> use echo in front of the functions, like so:
> <?php echo the_foo() ?>
>
> I tend to agree with Matt on that one.
> Which supports my case for having two separate functions with some kind
> of clear naming convention.
>
> There are no drawbacks to using my approach, unless you think a higher
> number of functions is a drawback. Personally, I think having two
> functions that have a consistent naming convention and a logical
> connection such as this is better than having one with an extra argument.
>
> I realize that the get_ prefix is used in many cases, so I am sure we
> can come up with a better one. The idea is to have template-function
> that echos the return value from prefix-template-function. The value of
> prefix can be determined later and is not the topic of discussion (until
> we all agree that this is the best approach :).
>
> Join the discussion!
>
> /Brian
>
>
> _______________________________________________
> hackers mailing list
> hackers at wordpress.org
> http://wordpress.org/mailman/listinfo/hackers_wordpress.org
>
----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.
More information about the hackers
mailing list