Recent Topics

b2evocore Request for Functions (Return Values)

Started by on Oct 24, 2005 – Contents updated: Oct 24, 2005

Oct 24, 2005 23:27    

Hey, in all my skins ive needed to mod the b2evocore,
And always for the same reason, to make it so i can add variables into a string instead of printing them.
Please add the following code to each function in the b2evocore, as it does not affect the function.

Changes are in BOLD

<?php

function functionName ($Variables = Values, $print = true)
{
//Do your stuff

if($print)
echo $output;
else
return $output;

}

?>

So now you can do:

<?php
$myString = "hello".functionName($Variables,false)."bye";
print($myString);
?>

Instead of:

hello
<?php functionName($Variables,false); ?>
bye

Using the return variable is very effecient if we want to mod the value, or use it inside variables.

Until you add this, im going to continue doing my hax.
-Balupton

Nov 28, 2005 14:15

Okay, quite a few functions already have a param named $disp (for display) which serves exactly that purpose.

Anyway, the way we'll go in the future is have all functions doubled like this:

function get_something( $params )
{
   return xxx;
}


function something( $params )
{
   echo get_something( $params );
}

Please list functions you need to be modified and we'll do. (Don't say "all" unless you want CVS access to do it yourself - in this case, contact me).

Nov 28, 2005 18:36

i dont see the point in doubling the functions, as code would be repeted and it would get rather messy, what i have done a few times is this:


function myFunc($var = '', $anothervar = '', $print = true){
   //etc
}
function return_myFunc(){
  return myFunc('','',false);
}


But then again no need in duplicating/adding extra functions.

What would be great is if there was a way of doing this:


function myFunc($var = '', $anothervar = '', $print){
   //etc
}
echo myFunc( { $print = false } );


So you dont need to specify each variable before $print.

And I would be keen to change all of them, i was thinking of making an application to do it for me anyway.

I also made another recomendation for new code for the tabbing in the cpanel.
You should have a b2evo updates forum ;)

Thanks
-balupton[/code]

Nov 28, 2005 19:10

balupton, having a function that prints / echos and uses the get_xxx() method would not double quote, but just the numbers of functions.

What would be great is if there was a way of doing this:
Code:

function myFunc($var = '', $anothervar = '', $print){
//etc
}
echo myFunc( { $print = false } );

You can do this only (AFAIK) with an array of params, like

function myFunc( $params = array() )
{
  if( empty($params['param1']) )
  {
    $params['param1'] = 'default value';
  }

  // ...

  if( empty( $params['print'] ) || $params['print']  )
  {
     echo $r;
   }
   return $r;
}


(which would default to print in this case). The drawback is, that you cannot use phpdoc to document the single params functions but the whole params array only and you'll have to manually set/check for default settings. It's convient therefor IMHO only for extended params (where you can have a lot of them.. ItemList is a good example, but does not use it for the about the first 20 params) ;)

I support the way Fran├žois describes it above, as you may have read between the lines.. ;)

Nov 29, 2005 04:17

Well for this:


function get_something( $params )
{
   return xxx;
}
function something( $params )
{
   echo get_something( $params );
}


You will need to duplicate the code inside both functions and just change echo to return.

The only way i see it working is if the default function returned the value and the echo function echod the result of the default function, but then the user can just do echo myfunc, instead of making a new function for it.

Yer all up i don't really see the point of having a function echo a result instead of returning as with a returned value the user can echo it, or can modify/use it how they want.
[/code]

Nov 29, 2005 18:25

balupton wrote:

Well for this:


function get_something( $params )
{
   return xxx;
}
function something( $params )
{
   echo get_something( $params );
}


You will need to duplicate the code inside both functions and just change echo to return.

If you just want to get a member you could use $Item->views for example. To echo it, use $Item->views().

However, I though more about functions that do a bit more. Like msgform_link() (where there's no get_msgform_link() yet, but it would work this way then, that msgform_link() echos get_msgform_link()).

Yer all up i don't really see the point of having a function echo a result instead of returning as with a returned value the user can echo it, or can modify/use it how they want.

Your right about this, but it's easier and cleaner to have template(!) functions that echo by default, but allow to only get it.

I'd also just needed getters, but can understand that it makes skins more readable and easier for the "skinners".

Nov 29, 2005 18:45

I'd also just needed getters, but can understand that it makes skins more readable and easier for the "skinners".

Hmmmm, I'm also a "skinner", so it really depends on your style of coding.

The main 2 coding styles which i have come accross are:


<?php echo "<b>$var</b>"; ?>



<b><?php echo $var; ?></b>


But all the skinner will need to do is just add 'echo' before the function, instead of just calling the function.
You should just add it the list of things skinners need to do, to make their skins work with Pheonix.

And wouldnt this be faster for the server as less things need to be loaded into memory?

-------------

In the pheonix release i can see a change already with the permalink functions:


permalink( $mode = '', $blogurl='' )
{
	echo $this->gen_permalink( $mode, $blogurl );
}

Nov 30, 2005 19:00

Balupton,

There is NO code duplication !

function something() just calls function get_something() and echoes the return value.

Nov 30, 2005 19:06

yer i figured that out a while ago ;)

Now we are debating the point of why we keep the echo functions as well as the return functions, when the developer can just type echo.

Nov 30, 2005 19:13

because the less PHP there is in the template, the more readable it is for the designer.


Form is loading...

powered by b2evolution CMS – This forum is powered by b2evolution CMS, a complete engine for your website.