Hello,
Are there any plans to add object type casting support in PHP?
For example:
$circle = (Circle) ShapeFactory::createShape('circle');
$circle->radius = 10;
echo $circle->getArea();
It would be great if this feature could be added to 5.5 :)
__
Raymond Irving
I suppose your cast is a conversion. Otherwise it is not needed at all
because PHP has no types.
-----Ursprüngliche Nachricht-----
Von: Raymond Irving [mailto:xwisdom@gmail.com]
Gesendet: Mittwoch, 24. April 2013 17:40
An: PHP Internals
Betreff: [PHP-DEV] Object Type Casting
Hello,
Are there any plans to add object type casting support in PHP?
For example:
$circle = (Circle) ShapeFactory::createShape('circle');
$circle->radius = 10;
echo $circle->getArea();
It would be great if this feature could be added to 5.5 :)
__
Raymond Irving
There are no concrete plans no.
Object casting is typically available to please the static type checker.
This concern does not exist in PHP since types are checked at runtime. So
my first question would be: Why do you need it ?
Other questions include: what would it do for up-casting, down-casting,
and also casting to incompatible objects? How will it handle private
properties?
Best,
Hello,
Are there any plans to add object type casting support in PHP?
For example:
$circle = (Circle) ShapeFactory::createShape('circle');
$circle->radius = 10;
echo $circle->getArea();It would be great if this feature could be added to 5.5 :)
__
Raymond Irving
--
Etienne Kneuss
http://www.colder.ch
Hi!
Are there any plans to add object type casting support in PHP?
For example:
$circle = (Circle) ShapeFactory::createShape('circle');
$circle->radius = 10;
echo $circle->getArea();
What is this supposed to do? I.e. what createShape returns and what
(Circle) operator is doing to it?
It would be great if this feature could be added to 5.5 :)
5.5 is in feature freeze, so it won't happen in any case.
--
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Hi,
I guess it would work like an inline type-hint.
Something along the lines of:
$circle = ShapeFactory::createShape('circle');
if (!$circle instanceof Circle)
{
throw …;
}
I would like something like this, since it saves quite some code.
Cheers
Jannik
Am Mittwoch, 24. April 2013 um 19:19 schrieb Stas Malyshev:
Hi!
Are there any plans to add object type casting support in PHP?
For example:
$circle = (Circle) ShapeFactory::createShape('circle');
$circle->radius = 10;
echo $circle->getArea();What is this supposed to do? I.e. what createShape returns and what
(Circle) operator is doing to it?It would be great if this feature could be added to 5.5 :)
5.5 is in feature freeze, so it won't happen in any case.
--
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
-----Original Message-----
From: Jannik Zschiesche [mailto:hello@apfelbox.net]
Sent: 24 April 2013 20:22Am Mittwoch, 24. April 2013 um 19:19 schrieb Stas Malyshev:
Hi!
Are there any plans to add object type casting support in PHP?
For example:
$circle = (Circle) ShapeFactory::createShape('circle');
$circle->radius = 10;
echo $circle->getArea();What is this supposed to do? I.e. what createShape returns and what
(Circle) operator is doing to it?It would be great if this feature could be added to 5.5 :)
If you want to add this, see https://wiki.php.net/rfc/howto
I guess it would work like an inline type-hint.
If you need an inline type assertion, you can use something like the following pattern:
class MyClass
{
public static function cast(MyClass $obj)
{
return $obj;
}
}
Then you have a MyClass "cast" or type assertion via:
$x = MyClass::cast($x); // this will throw if $x is not an instanceof MyClass
HTH
Rich
Richard Bradley
Tel : 020 7485 7500 ext 3230 | Fax : 020 7485 7575
softwire
Sunday Times Best Small Companies 2012 - 6th in the UK
Web : www.softwire.com | Addr : 325 Highgate Studios, 53-79 Highgate Road, London NW5 1TL
Softwire Technology Limited. Registered in England no. 3824658. Registered Office : 13 Station Road, London N3 2SB