Req #63268 [Opn->Sus]: Scalar Casting

From: Date: Fri, 27 Jul 2018 13:54:48 +0000
Subject: Req #63268 [Opn->Sus]: Scalar Casting
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-216502@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=63268&edit=1 ID: 63268 Updated by: cmb@php.net Reported by: klaussantana at gmail dot com Summary: Scalar Casting -Status: Open +Status: Suspended Type: Feature/Change Request Package: Unknown/Other Function Operating System: Any PHP Version: Irrelevant Block user comment: N Private report: N New Comment: This feature would certainly require discussion on the internals@ mailing list, and likely a follow up RFC[1]. For the time being, I'm suspending this ticket. [1] <https://wiki.php.net/rfc/howto> Previous Comments: ------------------------------------------------------------------------ [2012-10-26 22:43:19] klaussantana at gmail dot com It's the opposite instead. It'll allow more OO use in a more natural way... Like we use in some other languages like javascript: <script> document.write( 'Test'.bold() ); </script> We will be able to do the same with PHP: <?php echo "Test"->bold(); ?> AND, we will be able to change the - let's call prototype - class used to cast that string as object. The developers for this feature can do somethings to make this feature more reliable, like implementing some other core/spl classes like ArrayObject as follow: - BooleanObject - NumberObject (maybe IntegerObject, FloatObject, etc...) - StringObject - and so on... Plus, adding some functions to change the default class used to typecast anything to any other thing... Like adding a rule to the parser... This will make PHP much more powerful in "n" ways... The sky is no longer the limit. And of course, maybe, adding some required interfaces to these typecasting... like StringObjectInterface... And you'll be forced to implement it's methods... So you'll personalize some behavior, but you'll certainly have the same methods instead. This will be a good example: If you are building an HTML document you may register an HTMLStringObject class to be used when you typecast a string as an object... And when you cast "test"->bold() you'll have the result: <strong>test</strong> If you use HTML5StringObject instead it will result in: <span style="font- weight: bold;">test</span> If you use MarkdownStringObject instead it will result in: **test** and so on.... with HTMLStringObject or HTML5StringObject "test"->linksTo('http://some.url.com'); // <a href="http://some.url.com">test</a> with MarkdownStringObject "test"->linksTo('http://some.url.com'); // [test](http://some.url.com) Talking about the scripts relying on certain behavior is a lame excuse to not implement this feature... If you're a real coder you'll certainly test your subject before using it. Remake you scripts if needed... Don't ever slow down evolution because you are not in the mood to fix your buggy code. No offenses here... Sorry for all my misspelled words and some texts looking rough and offensive. They was not intended to be. Instead, my english needs to be more trained. ------------------------------------------------------------------------ [2012-10-25 03:36:03] xianrenb at gmail dot com I think there are scripts relying on the default behavior of scalar type casting. Changing the default behavior could break those scripts. If one wants to do something similar, he could write and use his own classes/methods instead of using scalar types and type casting. Using custom classes/methods are much more flexible as classes could be extended. On the other hand, introducing the new feature would make debugging much more difficult. PHP should continue the OO way, but not return to the non-OO way. I would strongly recommend not implementing this feature. ------------------------------------------------------------------------ [2012-10-12 21:47:09] klaussantana at gmail dot com And then something like this: register_casting_behavior( 'object', 'string', '::__toString' ); register_casting_behavior( 'object', 'int', '::__toInt' ); $S = new String('Teste'); echo $S; // will call __toString() method within $S and return the value... echo $S +1; // will call __toInt() method within $S and return the value... Callbacks would work somehow like this: * When you specify an array with 1st parameter as a class name string and 2nd as constructor it will instantiate the object. Else it will invoke the static method. * When you specify an array with 1st parameter as an object and 2nd as any method it will invoke that method within that object * If the origin type is "object" and the callback is a string that starts with "::" it will call the remaining callback string as a method in the object being casted as the destination type. ------------------------------------------------------------------------ [2012-10-12 21:20:23] klaussantana at gmail dot com Note that it'll be automatically chainable because the result of ->toLower() is a string and it will cast as an object... And you'll can also do something like this: echo 'My String'->toLower(); Just suggestions.. comment. ------------------------------------------------------------------------ [2012-10-12 21:16:36] klaussantana at gmail dot com Description: ------------ It would be nice if we can control the casting behavior of any type to another. Like a function like this: register_casting_behavior( $from, $to, $callback_function ); So when you do this: register_casting_behavior( 'int', 'bool', 'int2bool' ); $X = (bool) 1; It will call int2bool( 1 ) and so... Then.... Test script: --------------- register_casting_behavior( 'string', 'object', array( 'String', '__construct' ) ); class String { protected $value = null; public function __construct( $String ) { $this->value = $String; return $this; } public function toLower() { return strtolower($this->value); } public function underline() { return "<span style='text-decoration: underline;'>{$this->value}</span>"; } } $S = 'My String'; echo $S->toLower()->underline(); // will output: <span style='text-decoration: underline;'>my string</span> ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=63268&edit=1

« previous php.bugs (#216502) next »