re: I Want Scalar Objects in PHP VIEW POST

VIEW FULL DISCUSSION
 
<?php

class Name {
    private $name;

    function __construct($name) {
        $this->name = $name;
    }

    function length() {
        return mb_strlen($this->name, 'UTF-8');
    }

    /* object replace() */
    function ooReplace($needle, $replace) {
        $this->name = str_replace($needle, $replace, $this->name);
        return $this->name;
    }

    /* functional replace() */
    function replace($needle, $replace) {
        return str_replace($needle, $replace, $this->name);
    }

    function __toString() {
        return $this->name;
    }
}

$name = new Name("žluťásek");
var_dump((string) $name);                 // string(11) "žluťásek"
var_dump($name->length());                // int(8)
var_dump($name->replace('ťáse', 'ční'));  // string(10) "žlučník"
var_dump((string) $name);                 // string(11) "žluťásek"
 
code of conduct - report abuse