...

View Full Version : Class help needed : Can a method have more than one output ??



mOrloff
02-10-2010, 04:23 PM
I need two pcs of information which are found via nearly the same exact process as each other.
Instead of running the same chunk of code twice in two separate methods, I tried to just pass var into it for an if/else.

I am then actually calling it twice, but only getting output for whichever one is first.

Here's a snipet:


class PageIdentifier{
...
...
public function root($var=NULL){
if(strlen($this->parent)>0){ // if there is a parent
while($this->parent >0){ // keep looping until we reach the first generation
foreach($this->ary as $handle){ // loop through the info for all the pages
if($handle[pageID]==$this->parent){ // if the current loop is on the parent of this page ...
$tier='a bunch link of code';// set up the parent-page information for the root path
$rootPath=$tier.$rootPath; // prepend the parent-page title to the root path
$this->parent=$handle[parentID]; // "increment" the parent ID
if($this->parent==1){ // and if we've made it up to the generation just prior to "home", determine the appropriate CSS branch
switch($handle[pageName]){
case 'services.php':
$branch='services';
break;
case 'products.php':
$branch='products';
break;
...
...
default:
$branch='home';
} // end switch
} // end IF ($handle[parentID]==1) conditional
break;
} // end IF
} // end foreach loop
} // end WHILE ($this->parent >0) loop
} // end IF(strlen($this->parent)>0) conditional

if($var=='path'){ // determine which kind of output is required
echo $rootPath; // directly give the output of the root path links
}elseif($var=='cssBranch'){
echo $branch; // directly give the output of the CSS branch
} // close the IF/ELSEIF conditional
} // close the ROOT function


Then I try to use it like this:


<h2 id="root"><?PHP $page->root('path');?></h2>
<h3 id="pageName" class="<?PHP $page->root('cssBranch');?>">blah blah blah</h3>


And it gives the desired result for whichever one of those calls I put first.
The second one gets nothing.

What's the right way to structure what I'm trying to do ??

Thanks-a-bunch,
~ Mo

MattF
02-10-2010, 04:43 PM
Put the vars that are echoed in your class into an array, remove the echo and return the array and work from the array in your script. For example:



$output = array();

if($var=='path'){ // determine which kind of output is required
$output['path'] = $rootPath; // directly give the output of the root path links
}elseif($var=='cssBranch'){
$output['branch'] = $branch; // directly give the output of the CSS branch
}
return $output;


The logic seems weird in your code, somehow, but that's the general method which should work best if I've understood you correctly?

mOrloff
02-10-2010, 05:40 PM
... [your] logic seems weird ...
I can't say that that's surprising :D



Put the vars ... into an array, ... work from the array in your script
...
If I go that route, the I wouldn't even need the IF/ELSIF, right?
Just return the array and grab the appropriate element?
I'll give it a shot.

I know echoeing directly from a class is not a best-practice, but I have no use for what is getting returned other than to echo it, and would really like to keep my script code as clean as possible.

Thanks for the help,
Is there any other advice or suggestions out there for me on this?

Thanks-a-bunch,
~ Mo

MattF
02-10-2010, 06:00 PM
If I go that route, the I wouldn't even need the IF/ELSIF, right?
Just return the array and grab the appropriate element?
I'll give it a shot.

Pretty much. Just set a default false, empty string etc for each var when you initiate the class so that everything is actually set though. Saves running isset and suchlike later. You have a known default to check for.



I know echoeing directly from a class is not a best-practice, but I have no use for what is getting returned other than to echo it, and would really like to keep my script code as clean as possible.


Best practice has it's place. If echo/print does the job and has no detrimental effect, my motto is sod best practice. :D The array is simply easier, (and probably your only real choice), when you may be returning multiple values.

mOrloff
02-10-2010, 09:44 PM
Thanks



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum