Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts

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

    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:
    PHP Code:
    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:
    Code:
    <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

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    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:

    Code:
        $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?
    Last edited by MattF; 02-10-2010 at 03:49 PM.

  • #3
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by MattF View Post
    ... [your] logic seems weird ...
    I can't say that that's surprising

    Quote Originally Posted by MattF View Post

    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
    Last edited by mOrloff; 02-10-2010 at 04:45 PM.

  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by mOrloff View Post
    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. The array is simply easier, (and probably your only real choice), when you may be returning multiple values.

  • #5
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts
    Thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •