PHP模拟asp.net的StringBuilder类实现方法

yipeiwu_com6年前PHP代码库

本文实例讲述了PHP模拟asp.net的StringBuilder类实现方法。分享给大家供大家参考。具体如下:

在asp.net开发开发环境中,有一个StringBuilder类是比较常用的, 这个类用起来可以实现很方便的text文本的操作. 但是在php中,没有这个类. 不过我们却可以通过自定义类来模拟这个方法.

/******************************************** 
 * 
 * 函数名:StringBuilder 
 * 作 用:构造PHP下的StringBuilder类 
 * 
 ********************************************/
class StringBuilder 
{ 
  const LINE="<br/>"; 
  protected $list= array(''); 
  public function __construct( $str=NULL) 
  { 
    array_push($this->list,$str); 
  } 
  public function Append($str) 
  { 
    array_push($this->list,$str); 
    return $this; 
  } 
  public function AppendLine($str) 
  { 
    array_push($this->list,$str.self::LINE); 
    return $this; 
  } 
  public function AppendFormat( $str,mixed $args) 
  { 
    array_push($this->list, sprintf($str,$args)); 
    return $this; 
  } 
  public function ToString() 
  { 
    return implode("",$this->list); 
  } 
  public function __destruct() 
  { 
    unset($this->list); 
  } 
}

希望本文所述对大家的php程序设计有所帮助。

相关文章

PHP常用的三种设计模式汇总

本篇文章是学习PHP中常用的三种设计模式的笔记及总结,不管采用哪一门语言开发什么,几乎都会使用到设计模式,我们为什么需要设计模式呢?它的诞生对于我们开发人员来说有什么样的作用与意义呢?...

Php Cookie的一个使用注意点

复制代码 代码如下:<?php setcookie('test', 'this is a cookie test'); echo ($_COOKIE['test']); ?>...

关于php fread()使用技巧

说明 string fread ( int handle, int length ) fread() 从文件指针 handle 读取最多 length 个字节。该函数在读取完最多 len...

php字符串函数学习之substr()

<?php /* 定义和用法 substr() 函数返回提取的子字符串, 或者在失败时返回 FALSE。 语法 substr(string,start,length) 参数...

php获得当前的脚本网址

//获得当前的脚本网址 function GetCurUrl(){     if(!empty($_SERVER["REQUEST_...