php提供了一个叫_toString()的函数,可以用来返回表示对象的字符串信息,一旦定义,打印命令将调用它并打印出返回的字符串。

<?php
class Person{
  function __construct($name){
    $this->name=$name;
  }
  function __toString(){
    return $this->name;
  }
  private $name;
}
$obj=new Person("Andi Gutmans");
echo $obj;
echo '<br>';
print $obj;
?>

结果:

Andi Gutmans

Andi Gutmans

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-24
  • 2021-11-30
  • 2021-11-28
  • 2021-11-28
  • 2021-11-28
  • 2021-11-28
猜你喜欢
  • 2022-12-23
  • 2022-03-09
  • 2021-11-28
  • 2021-08-12
  • 2021-11-24
  • 2022-12-23
相关资源
相似解决方案