ID:               46128
 Comment by:       rayro at gmx dot de
 Reported By:      131 dot php at cloudyks dot org
 Status:           Open
 Bug Type:         Feature/Change Request
 Operating System: Linux
 PHP Version:      5.2.6
 New Comment:

This is such a nice Implementation and very useful, but i prefer to add
the methods __toBool, __toInt and __toArray rather than __cast, stay
tuned with PHP's other magic methods..

I dont agree fully with that post in feature request #38508 from helly.
If that'll be the way, why did the decision for magic methods?
Isnt that all complex although? ^^
We know, but in my eyes (and many others), i think that these magically
stuff is one of the top key features for php. And none of these features
will become critism i think. If not needed, just dont use them!

The additional goody __toInvoke() introduced in 5_3 is a such nice
addition for developing "quick gets" based on nested object sets:
<?php
$magazine(3)->getArticles();
// old way
$magazine->getArticle($magazine->useMagazine(3));
?>
This helper is a "backdoor" like way to enable PHP to fully write
nested Objects like in Javascript. And my opinion for that: I LIKE THIS
:)

I think this is a very very discussable topic to the changes for 5_3 or
6_0 beside the wanted support for traditional type hinting and utf8!

thanks


Previous Comments:
------------------------------------------------------------------------

[2008-11-23 09:01:02] mark at hell dot ne dot jp

Please test the following extension :

http://ookoo.org/svn/snip/phpcastable/

This extension adds a "Castable" interface. Any class implementing this
interface have to implement a __cast() function. This function will be
called when the object needs to be casted to a type.

This extension is EXPERIMENTAL and needs more testing/review before
being used in any production system.

------------------------------------------------------------------------

[2008-10-27 17:08:24] info at netmosfera dot it

AWESOME!
php developers, please, we need it!!
http://bugs.php.net/bug.php?id=46404

------------------------------------------------------------------------

[2008-10-06 04:15:03] mark at hell dot ne dot jp

Backported to PHP 5.2.6 :

http://ookoo.org/svn/snip/php-5.2.6_class_cast_func.patch

------------------------------------------------------------------------

[2008-10-05 16:33:07] 131 dot php at cloudyks dot org

This is awesome

------------------------------------------------------------------------

[2008-10-05 08:49:55] mark at hell dot ne dot jp

Did a test implementation only supporting a few types (int, float,
bool, string).

This works as expected.

Test code:
----------
class test {
        public function __cast($type) {
                switch($type) {
                        case 'string': return 'a string';
                        case 'bool': return true;
                        case 'float': return 123.456;
                        case 'int': return 978;
                        default:
                                var_dump($type);
                                return null;
                }
        }
}

$t = new test();

var_dump((string)$t);
var_dump((int)$t);
var_dump((float)$t);
var_dump((bool)$t);

Result:
string(8) "a string"
int(978)
float(123.456)
bool(true)

Here's the patch file for this simple implementation against PHP
5.3.0alpha2.

http://ookoo.org/svn/snip/php-5.3.0alpha2_class_cast_func.patch

------------------------------------------------------------------------

The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
    http://bugs.php.net/46128

-- 
Edit this bug report at http://bugs.php.net/?id=46128&edit=1

Reply via email to