dougao1542 2013-04-11 05:27
浏览 20

在php对象中保存内存

I have a class in php with a method similar to the two below. I am wondering what is the most optimal way to write this class? I need to use the disp_info method a lot and would like to know if it is wiser to place $message1 and $message2 outside of the function and call it in so I would not have to generate a new message every time or the other way around.

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();
}

Thanks, Please enlighten me

  • 写回答

1条回答 默认 最新

  • dongou4052 2013-04-11 05:46
    关注

    I can't think of any reason for using static variables in a class. It even somehow contradicts the whole concept of classes. Generally, you better won't use the first method. About optimization - the different would be measured in ms , the question is about which method would run faster - you could easly run a test and measure runtime of both methods.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?