Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Foo {
- public $string = '';
- //public $length = 0;
- public function __get (string $name) {
- if($name === 'length') return strlen($this->string);
- $trace = debug_backtrace();
- trigger_error(
- 'Undefined property via __get(): ' . $name .
- ' in ' . $trace[0]['file'] .
- ' on line ' . $trace[0]['line'],
- E_USER_NOTICE);
- return null;
- }
- public function __isset ( string $name ) : bool {
- if($name === 'length') return true;
- return false;
- }
- public function __debugInfo() : array {
- return [
- 'length' => $this->length,
- 'string' => $this->string
- ];
- }
- public function addCharsAt(int $index, string $chars) {
- if(strlen($chars) === 0 || $index < 0 || $index > 1 + $this->length) return null;
- $str='';
- $len = $this->length;
- for ($i = 0; $i <= $len;) {
- if ($i === $index) {
- $str .= $chars;
- $index = -1;
- //$this->length += strlen($chars);
- } elseif ($i < $len) {
- $str .= $this->string[$i];
- $i++;
- } else break;
- }
- $this->string=$str;
- return null;
- }
- }
- $foo = new Foo();
- $foo->addCharsAt(0, "test");
- var_dump($foo);
- $foo->addCharsAt(2, "xx");
- var_dump($foo);
- $foo->addCharsAt(6, "yy");
- var_dump($foo);
Add Comment
Please, Sign In to add comment