Skip to content

Instantly share code, notes, and snippets.

@kobus1998
Created January 6, 2020 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kobus1998/482f22a7c8203d9e35233725f4392b04 to your computer and use it in GitHub Desktop.
Save kobus1998/482f22a7c8203d9e35233725f4392b04 to your computer and use it in GitHub Desktop.
object scalar string
<?php
class Str
{
private $str;
public function __construct(string $str)
{
$this->str = $str;
}
public function replace($a, $b)
{
$this->str = str_replace($a, $b, $this->str);
return $this;
}
public function ireplace($a, $b)
{
$this->str = str_ireplace($a, $b, $this->str);
return $this;
}
public function toUpper()
{
$this->str = strtoupper($this->str);
return $this;
}
public function toLower()
{
$this->str = strtolower($this->str);
return $this;
}
public function append($str)
{
$this->str .= $str;
return $this;
}
public function prepend($str)
{
$this->str = $str . $this->str;
return $this;
}
public function trim()
{
$this->str = trim($this->str);
return $this;
}
public function pregReplace($preg, $str)
{
$this->str = preg_replace($preg, $str, $this->str);
}
public function get()
{
return $this->str;
}
public function __toString()
{
return $this->str;
}
}
$str = (string) (new Str('Hello World'))
->toUpper()
->ireplace('world', '')
->trim()
->append('!')
;
echo $str; // HELLO!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment