RE: [PHP-DEV] Object Type Casting

From: Date: Thu, 25 Apr 2013 08:46:03 +0000
Subject: RE: [PHP-DEV] Object Type Casting
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-67141@lists.php.net to get a copy of this message
> -----Original Message----- > From: Jannik Zschiesche [mailto:hello@apfelbox.net] > Sent: 24 April 2013 20:22 > > 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 :) 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

« previous php.internals (#67141) next »