note 94310 added to intro.spl-types

From: Date: Wed, 28 Oct 2009 16:09:43 +0000
Subject: note 94310 added to intro.spl-types
Groups: php.notes 
Request: Send a blank email to php-notes+get-162192@lists.php.net to get a copy of this message
For backwards compatibility issues: We do know that auto-unboxing works for strings, thanks to the __toString() magic function. Auto-unboxing for other scalar types may be tricky but possible, by using the same function with another return type (which of cause has some limitations). However auto-boxing is a real challenge. If you can provide any details, I would appreciate that. For auto-unboxing see this code-fragment: class MyString { private $value = ""; public function __construct($string) { if (!is_string($string)) { throw new InvalidArgumentException(); } $this->value = $string; } public function __toString() { return $this->value; } } For other scalar types, all that changes is the constructor function. Anything else remains unchanged. (While you are at it, you may want to add some type conversion functions for your personal convenience.) HowTo: function foo(MyString $string) { print "$string"; // auto-unboxing in action } // prints "Hello World!" foo(new MyString("Hello World!")); ---- Server IP: 212.124.37.9 Probable Submitter: 80.69.35.102 ---- Manual Page -- http://www.php.net/manual/en/intro.spl-types.php Edit -- https://master.php.net/note/edit/94310 Del: integrated -- https://master.php.net/note/delete/94310/integrated Del: useless -- https://master.php.net/note/delete/94310/useless Del: bad code -- https://master.php.net/note/delete/94310/bad+code Del: spam -- https://master.php.net/note/delete/94310/spam Del: non-english -- https://master.php.net/note/delete/94310/non-english Del: in docs -- https://master.php.net/note/delete/94310/in+docs Del: other reasons-- https://master.php.net/note/delete/94310 Reject -- https://master.php.net/note/reject/94310 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#162192) next »