|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #63268 Scalar Casting
Submitted: 2012-10-12 21:16 UTC Modified: 2018-07-27 13:54 UTC
Avg. Score:3.7 ± 1.9
Reproduced:0 of 0 (0.0%)
From: klaussantana at gmail dot com Assigned:
Status: Suspended Package: Unknown/Other Function
PHP Version: Irrelevant OS: Any
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2012-10-12 21:16 UTC] klaussantana at gmail dot com
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...


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>


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-10-12 21:20 UTC] 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:47 UTC] 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 
 [2012-10-25 03:36 UTC] 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 
PHP should continue the OO way, but not return to the non-OO way.
I would strongly recommend not implementing this feature.
 [2012-10-26 22:43 UTC] 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:

   document.write( 'Test'.bold() );

We will be able to do the same with 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 
 - 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 

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 

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
// <a href="">test</a>

with MarkdownStringObject
// [test](

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 

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.
 [2018-07-27 13:54 UTC]
-Status: Open +Status: Suspended
 [2018-07-27 13:54 UTC]
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] <>
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Wed Sep 28 10:03:42 2022 UTC