Edit report at https://bugs.php.net/bug.php?id=63268&edit=1

 ID:                 63268
 Comment by:         xianrenb at gmail dot com
 Reported by:        klaussantana at gmail dot com
 Summary:            Scalar Casting
 Status:             Open
 Type:               Feature/Change Request
 Package:            Unknown/Other Function
 Operating System:   Any
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[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

Reply via email to