Re: Pseudo-objects (methods on arrays, strings, etc.)
| From: | David Muir | Date: | Fri, 20 Jul 2012 00:51:01 +0000 |
| Subject: | Re: Pseudo-objects (methods on arrays, strings, etc.) | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-61535@lists.php.net to get a copy of this message | ||
On 20/07/12 09:38, Andrew Faulds wrote:
> I'm curious, how do I make my objects have scalar passing semantics, then?
> On Jul 20, 2012 12:35 AM, "Sara Golemon" <pollita@php.net> wrote:
>
How about this?
class String {
protected $string='';
public function __construct($string){
$this->string = $string;
}
public function toUpper(){
$string = clone $this;
$string->string = strtoupper($this->string);
return $string;
}
public function __toString(){
return $this->string;
}
}
$foo = new String('foo');
$bar = $foo->toUpper();
echo $foo; //foo
echo $bar; //FOO
So far we only have the toString magic method. If we had the others, you
could conceivably do this all in userland (at a fairly high performance
cost I would assume).
Cheers,
David