jeskey boneman
jeskey boneman

Reputation: 227

call function to function by variables

Hello I tried to echo the output of a function by another function and I wrote this code:

$users = new user();
class user{
  public function userlist(){
    //Here is my sql action and then it send the result as output
    echo $output;
  }
}

Now I have a class which contains the template of the site, this file passes through every pages.

$html = new html();
class html{
  public function output($output){
    //echoing some html styles and the template html code..
    echo '<div id="body">';
    $output;
    echo "</div>";
  }
}

Now the $output should be in the html page inside the <div id="body">, the problem is that $output's HTML is echoed before the div and is not inside the div.

Upvotes: 0

Views: 48

Answers (3)

didierc
didierc

Reputation: 14730

The simple way:

class user{
  public function userlist(){
    //Here is my sql action and then it send the result as output
    return $output;
  }
}

class html{
  public function output($output){
    //echoing some html styles and the template html code..
    echo '<div id="body">';
    echo $output;
    echo "</div>";
  }
}

class controller {
  function handler(){
    $users = new user();
    $html = new html();
    $html->output($users->userlist());
  }
}

Now without modifying the user class, you could use output buffering to temporary hold the $users object output, and then output it at a more convenient time:

class user{
  public function userlist(){
    //Here is my sql action and then it send the result as output
    echo $output;
  }
}

class controller {
  function handler(){
    $users = new user();
    $html = new html();
    ob_start();
    $users->userlist();
    $output = ob_get_contents();
    ob_end_clean();
    $html->output($output);
  }
}

I don't think you can get the expected result without modifying the html::output method however.

See the documentation on the ob_* functions for an explanation of their semantics.

Addendum: please note that many frameworks already use output buffering internally. The documentation states that buffers are stackable (ie. you may start a new buffering on top of another running one), but just be careful to start and end your buffers properly so as to not mess up your framework inner state (and ultimately your output), if you are using one.

Upvotes: 1

Fluffeh
Fluffeh

Reputation: 33502

You are accessing the variable incorrectly. As it is a string, you should simply append or concatenate it within the output as follows:

class html{
public function($output){
    //echoing some html styles and the template html code..
    echo '<div id="body">'.$output."</div>";
    }
}

Additionally, in your first function, you simply output the value of $output rather than actually save it to a variable. If you want to display things in order, you should either save the data to a variable you can access properly elsewhere (say in a return statement) or output your initial stream, then run the function, then output the remaining stream.

Upvotes: 0

Saqueib
Saqueib

Reputation: 3520

You need to echo $output also

public function($output){
 //echoing some html styles and the template html code..
  echo '<div id="body">';
  echo $output;
  echo "</div>";
}

or use just one liner

public function($output){
  //echoing some html styles and the template html code..
  echo '<div id="body">'. $output . "</div>";
}

Upvotes: 0

Related Questions