从类外部回显php变量但在html标签内

My problem is:

How can access I variable from outside class like so below?

Please someone give me a hint because I don't want to echo html inside PHP but PHP inside the HTML.

<div class='comment-text'>
    <?php                     
    echo $commento; 
            ?>
         </div>
  class Posts{
   public function __construct(){
       $this->connP = new connection2();
       $this->connP = $this->connP->dbconnection();
  }
   public function DETAIL_p(){
$sql_cmmnt = "SELECT * FROM tbl_main_comments WHERE POST_ID = ?";
             $sql_cmmnt_statement    =$this->connP->prepare($sql_cmmnt);
                      $sql_cmmnt_statement->bindParam(1,$posta);
                      $sql_cmmnt_statement->execute();
                       while($row2=$sql_cmmnt_statement->fetch()){

                          $commento = $row2['comments'];    
                          $name_f = $row2['fname']; 
                            $name_s = $row2['sname'];   
                            $commentID = $row2['comment_id'];   



         $date_comment_posted =    $row2['date_time_commented'];
          $time = date("d M Y", strtotime($date_comment_posted));

                       }





                              }  
                     }else{echo("error");}
                               //END OF SQL

 }else{
 echo("oops! something went wrong try again later");
}
       }
    }
 $objectP=new Posts();
 $objectP->DETAIL_p();              

You should store the $commento variable in your Posts class. Then create a function to return it, and call that function

class Posts {
private $commento;
public function getComment (){ echo $commento }

public function DETAIL_p() {
//other function code
}

Then later you can call it in your HTML.

<?php
$object->getComment();
?>