我在 php 中有一个类,其方法类似于下面的两个。我想知道编写这个类的最佳方法是什么?我需要经常使用 disp_info 方法,并且想知道将 $message1 和 $message2 放在函数之外并调用它是否更明智,这样我就不必每次都生成新消息或反过来.
class Person(){
public $name;
public $age;
public $sex;
public $result;
public function disp_info(){
$message1= "Hello my name is : ";
$message2= "And my age is :";
$this->result= $message1.$this->name.$message2.$this->age;
}
$this->disp_name();
}
class Person(){
public $name;
public $age;
public $sex;
public $result;
public $message1= "Hello my name is : ";
public $message2= "And my age is :";
public function disp_info(){
$message1 = $this->message1
$message2 = $this->message2
$this->result = $message1.$this->name.$message2.$this->age;
}
$this->disp_name();
}
谢谢,请赐教