Skip to content

Instantly share code, notes, and snippets.

@horitaku1124
Created May 4, 2014 17:26
Show Gist options
  • Save horitaku1124/6484cb10d91d1e0f0116 to your computer and use it in GitHub Desktop.
Save horitaku1124/6484cb10d91d1e0f0116 to your computer and use it in GitHub Desktop.
PHP Scalar Objectのテスト
<?php
class StringExtension {
public function replace($search, $replace) {
return str_replace($search, $replace, $this);
}
public function preg_replace($pattern, $replacement) {
return preg_replace($pattern, $replacement, $this);
}
public function indexOf($needle, $offset = 0) {
$index = strpos($this, $needle, $offset);
if($index === false) return -1;
return $index;
}
public function length() {
return strlen($this);
}
public function split($delimiter) {
return explode($delimiter, $this);
}
}
class ArrayExtension {
public function length() {
return count($this);
}
public function join($delimiter) {
return implode($delimiter, $this);
}
}
register_primitive_type_handler('string', 'StringExtension');
register_primitive_type_handler('array', 'ArrayExtension');
$s = "abcdefg";
echo $s->replace("a", "A")->preg_replace('/[ceg]/', "?")->split("?")->join("!")->indexOf("f");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment