|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-10-12 21:16 UTC] 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>
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 15:00:01 2025 UTC |
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.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.