title: Can I save a PHP class method output into a variable
body: <p>I am doing a fansite of a game where I want to show tooltips for items. I use Bootstrap tooltips to generate them. So far I've been doing tooltips for items with arrays storing the data and then display it with the BS tooltips. Now, I want to transition to OOP and i want to create classes. I am using methods inside the classes to generate the tooltip. However I have a real issue calling the method. Let me give you an example.</p>
  
  <p>Current method:</p>
  
  <pre><code>// General variables
  $rune_item_level = "&lt;span class='white-tooltip'&gt;Item Level: 1&lt;br&gt;";
  $inserted_into_sockets = "Can be inserted into socketed items&lt;br&gt;&lt;br&gt;";
  $rune_class = "class='rune-image'";
  // El Rune
  $el_stats = [
      "Weapons" =&gt; "+50 To Attack Rating, +1 to Light Radius",
      "Armor" =&gt; "+15 Defense, +1 to Light Radius",
      "Helms" =&gt; "+15 Defense, +1 to Light Radius",
      "Shields" =&gt; "+15 Defense, +1 to Light Radius"
  ];
  $el_rune_tooltip = "&lt;span class='rune-tooltip'&gt;El Rune&lt;/span&gt;&lt;br&gt;";
  $el_rune_tooltip .= $rune_item_level;
  $el_rune_tooltip .= $inserted_into_sockets;
  foreach ($el_stats as $key=&gt;$value){
      $el_rune_tooltip.= "&lt;span class='white-tooltip'&gt;" . $key . ": " . $value . "&lt;/span&gt;";
      $el_rune_tooltip.= "&lt;br&gt;";
  }
  $el_rune_tooltip .= "&lt;br&gt;&lt;span class='white-tooltip'&gt;Required Level 11";
  $el_rune = '&lt;img ' . $rune_class . ' src="/images/runes/El_Rune.png" data-toggle="tooltip" data-html="true" title="' . $el_rune_tooltip . '" /&gt;';
  </code></pre>
  
  <p>It works well. I store the whole tooltip in into the $el_rune variable and I call whenever I want to and anywhere.</p>
  
  <p>So transitioning to OOP i did the following:
  I create a Rune class, pass some properties and then i use a function called createTooltip() to create the tooltip. This way my code is significantly lighter creating all 30+ runes in the game.</p>
  
  <pre><code>    class Rune {
      public $name, $stats, $image;
      public function __construct($name, $stats, $image){
          $this-&gt;name = $name;
          $this-&gt;stats = $stats;
          $this-&gt;image = $image;
          $this-&gt;rune_ilvl = 1;
      }
      public function createTooltip() {
              $tooltip = "&lt;span class='rune-tooltip'&gt;" . $this-&gt;name . "&lt;/span&gt;&lt;br&gt;&lt;span class='white-tooltip'&gt;Item Level: 1&lt;br&gt;Can be inserted into socketed items&lt;br&gt;&lt;br&gt;";
              foreach ($this-&gt;stats as $key=&gt;$value){
                  $tooltip .= "&lt;span class='white-tooltip'&gt;" . $key . ": " . $value . "&lt;/span&gt;";
                  $tooltip .= "&lt;br&gt;";
              }
              $tooltip .= "&lt;br&gt;&lt;span class='white-tooltip'&gt;Required Level " . $this-&gt;rune_ilvl . "&lt;/span&gt;";
              echo '&lt;img class="rune-image" src="' . $this-&gt;image . '" data-toggle="tooltip" data-html="true" title="' . $tooltip . '" /&gt;';
      }
  }
  $el_rune_oop = new Rune("El", $el_stats, "/images/runes/El_Rune.png");
  </code></pre>
  
  <p>So my problem is this. Before, whenever I want to call the image of the Rune item with the cool tooltip, i just do "echo $el_rune". Now if i want to do the same i need to echo $el_rune_oop->createTooltip(). It does the same job. But i have problems calling it or do cool stuff with it where with the variable, i can include it into new arrays and new structures and it's easy to remember... the $el_rune_oop->createTooltip() executes whenever i mention it anywhere, causing all sort of issues for me. I tried saving $el_rune_oop->createTooltip() itself into a variable which would be a life saver but that of course doesn't work.</p>
  
  <p>Is there any salvation for me or just carry on not using OOP methods?</p>