Autoboxing in php 5.1

php.internals

Ante Drnasin

21 years ago
I was wondering if anyone can comment on this idea cause I think it would be a great asset to PHP and to OOP in PHP.. example: $b = 4; $a = new Integer(4); if($a == $b) { //bla bla } Other classes would include String(), Integer(), Boolean() ... and so on...

Marcus Börger

21 years ago
Hello Ante, autoboxing is an ugly java 1.5 workarount to enable storage of base tyoes in containers. There this trick is necessary because as always in java everything must be an 'Object'. In contrast PHP is a loosly typed language where a container aka array can store any type. Apart from that as with java there is no user space enabled way to implement such autoboxing types and even the java way of building it into the engine is not supported and won't be. regards marcus Monday, February 7, 2005, 3:21:59 PM, you wrote:
> I was wondering if anyone can comment on this idea > cause I think it would be a great asset to PHP and to OOP in PHP..
> example:
> $b = 4; > $a = new Integer(4);
> if($a == $b) { > //bla bla > }
> Other classes would include String(), Integer(), Boolean() ... and so on...
-- Best regards, Marcus mailto:helly@php.net

Ante Drnasin

21 years ago
Hi Marcus and thanx for the explanation.... And I agree that PHP is very loose which is his strong part but exactly because of that I think it would be nice if the end-user can have the ability to work with primitives like they were objects... I didn't want to suggest that PHP should implement Autoboxing as JAVA but I did like the concept of having objects for primitive types.... Marcus Boerger wrote:

Andrei Zmievski

21 years ago
On Wed, 09 Feb 2005, Ante Drnasin wrote:
> Hi Marcus and thanx for the explanation.... > > And I agree that PHP is very loose which is his strong part but > exactly because of that I think it would be nice if the end-user can have > the ability to work with primitives like they were objects...
Why? - Andrei

Ante Drnasin

21 years ago
Andrei Zmievski wrote:
> On Wed, 09 Feb 2005, Ante Drnasin wrote: > >>Hi Marcus and thanx for the explanation.... >> >>And I agree that PHP is very loose which is his strong part but >>exactly because of that I think it would be nice if the end-user can have >>the ability to work with primitives like they were objects... > > > Why? > > - Andrei
Why not? Because it would be a great asset to the new OOP model in PHP 5.... Because the new OOP model is great and I don't think it shouldn't stop there.... Because it would be great if the choice is left to the end user... We already have OOP freaks (including this one) and I just love the idea to have something like function Add($a, $b) { return new Float($a+$b); } print(Add(new Integer(5), new Float(5.7))); just my $.02

Ron Korving

21 years ago
Man, that's the primary reason I don't like Java :) You can't be serious :) Ron "Ante Drnasin" <ante.dfg@moj.net> wrote in message news:20050210080546.52065.qmail@lists.php.net...
> Andrei Zmievski wrote: > > On Wed, 09 Feb 2005, Ante Drnasin wrote: > > > >>Hi Marcus and thanx for the explanation.... > >> > >>And I agree that PHP is very loose which is his strong part but > >>exactly because of that I think it would be nice if the end-user can
have

Derick Rethans

21 years ago
On Thu, 10 Feb 2005, Ante Drnasin wrote:
> Andrei Zmievski wrote: > > On Wed, 09 Feb 2005, Ante Drnasin wrote: > > > >>Hi Marcus and thanx for the explanation.... > >> > >>And I agree that PHP is very loose which is his strong part but > >>exactly because of that I think it would be nice if the end-user can have > >>the ability to work with primitives like they were objects... > > > > > > Why? > > > > - Andrei > > Why not? > > Because it would be a great asset to the new OOP model in PHP 5.... > Because the new OOP model is great and I don't think it shouldn't stop > there.... > Because it would be great if the choice is left to the end user...
But there is no good reason why we should add this.
> > We already have OOP freaks (including this one) and I just love the idea > to have something like > > function Add($a, $b) { > return new Float($a+$b); > }
function add($a, $b) { return (float)($a + $b); } nothing new to add here... Derick
-- Derick Rethans http://derickrethans.nl | http://ez.no | http://xdebug.org

Andrei Zmievski

21 years ago
On Thu, 10 Feb 2005, Ante Drnasin wrote:
> >Why? > > > >- Andrei > > Why not? > > Because it would be a great asset to the new OOP model in PHP 5....
You did't answer my question. Why?
> Because the new OOP model is great and I don't think it shouldn't stop > there....
Why?
> Because it would be great if the choice is left to the end user...
Why?
> > We already have OOP freaks (including this one) and I just love the idea > to have something like > > function Add($a, $b) { > return new Float($a+$b); > }
Why?
> > print(Add(new Integer(5), new Float(5.7)));
Why? - Andrei

John Coggeshall

21 years ago
> You did't answer my question. Why?
I am def. a fan of this idea. I'd love to see internally a set of Java-style objects representing the basic types in PHP. As for why I have two reasons: Although PHP is not a strongly-typed language and never will be, with the introduction of type-hinting I feel that having a standard set of objects representing the fundamental types in PHP will allow developers of libraries to enforce the proper restrictions if they desire on the types which end up in their functions. This further degree of control over the architecture in a PHP class makes an architecture tighter and easier to manage with growth. More importantly than the concept of type-hinting, the lack of typing information is holding PHP back signifcantly in the realm of Web Services. Although for PHP applications as a whole typing has proven itself largely unnecessary, without it making PHP a viable and "simple" language for the creation of web services isn't feasible. How am I to create a WSDL document from a class I would like to expose as a web service? Do you honestly expect me to hand-write a WSDL document for my object and maintain that WSDL alongside code changes manually? WSDL documents were designed to be generated automatically by the architecture exposing the web service, and without any notion of typing in PHP at all there is no viable way to really do this. Also let me point out that I am *not* suggesting that PHP become a typed language. I am merely am suggesting that the ability for me to enforce structure on my objects does indeed have a real benefit without breaking backward compatibility or otherwise compromising the spirit of PHP. John

John Coggeshall

21 years ago
To be clear: Although I think this might be implemented as some sort of object I am not interested in making objects out of everything. All I want is this: function foo(Integer $a, Float $b, String $c, Boolean $d) { } and be able to introspect against that... how that ultimately gets implemented (likely PHP would auto-cast that to an int) doesn't matter to me... but the point here is I think the ability to have more *optional* meta-data associated with function declarations shouldn't be brushed off as nonsense when there are real benefits to the ability. John On Thu, 2005-02-10 at 15:51, John Coggeshall wrote:

Sara Golemon

21 years ago
> function foo(Integer $a, Float $b, String $c, Boolean $d) { > > } >
Having that automagically translate to: function foo($a, $b, $c, $d) { set_type($a, 'int'); set_type($b, 'float'); set_type($c, 'string'); set_type($d, 'bool'); } Is something that I could see building into the language (in all honesty, isn't that was zend_parse_parameters does for internal functions already?), but objects for primitives? Didn't someone say something about smelly corpses recently? maybe it was "charred"? cold? smoking? rank? -Sara

John Coggeshall

21 years ago
> Is something that I could see building into the language (in all honesty, > isn't that was zend_parse_parameters does for internal functions already?), > but objects for primitives? Didn't someone say something about smelly > corpses recently? maybe it was "charred"? cold? smoking? rank?
I don't want objects for primitives, at least that's not where I was going. I am only arguing the typehinting of primitives. John

Ante Drnasin

21 years ago
John Coggeshall wrote:
> To be clear: > > Although I think this might be implemented as some sort of object I am > not interested in making objects out of everything. All I want is this: > > function foo(Integer $a, Float $b, String $c, Boolean $d) { > > } > > and be able to introspect against that... how that ultimately gets > implemented (likely PHP would auto-cast that to an int) doesn't matter > to me... but the point here is I think the ability to have more > *optional* meta-data associated with function declarations shouldn't be > brushed off as nonsense when there are real benefits to the ability. > > John > > On Thu, 2005-02-10 at 15:51, John Coggeshall wrote: > >>>You did't answer my question. Why? >> >>I am def. a fan of this idea. I'd love to see internally a set of >>Java-style objects representing the basic types in PHP. >> >>As for why I have two reasons: >> >>Although PHP is not a strongly-typed language and never will be, with >>the introduction of type-hinting I feel that having a standard set of >>objects representing the fundamental types in PHP will allow developers >>of libraries to enforce the proper restrictions if they desire on the >>types which end up in their functions. This further degree of control >>over the architecture in a PHP class makes an architecture tighter and >>easier to manage with growth. >> >>More importantly than the concept of type-hinting, the lack of typing >>information is holding PHP back signifcantly in the realm of Web >>Services. Although for PHP applications as a whole typing has proven >>itself largely unnecessary, without it making PHP a viable and "simple" >>language for the creation of web services isn't feasible. How am I to >>create a WSDL document from a class I would like to expose as a web >>service? Do you honestly expect me to hand-write a WSDL document for my >>object and maintain that WSDL alongside code changes manually? WSDL >>documents were designed to be generated automatically by the >>architecture exposing the web service, and without any notion of typing >>in PHP at all there is no viable way to really do this. >> >>Also let me point out that I am *not* suggesting that PHP become a typed >>language. I am merely am suggesting that the ability for me to enforce >>structure on my objects does indeed have a real benefit without breaking >>backward compatibility or otherwise compromising the spirit of PHP. >> >>John
Exactly!

Andi Gutmans

21 years ago
I think phpDoc is the solution because especially as we start going into the web services realm we're going to need to document more complex signatures and this kind of syntax won't be sufficient anyway. We will need to have a way to document web services so that we can auto-generate WSDL files. Andi At 09:43 AM 2/11/2005 +0100, Ante Drnasin wrote:

Sean Coates

21 years ago
Andi Gutmans wrote:
> I think phpDoc is the solution because especially as we start going into > the web services realm we're going to need to document more complex > signatures and this kind of syntax won't be sufficient anyway. > We will need to have a way to document web services so that we can > auto-generate WSDL files.
It would be nice to be able to (optionally) parse phpdoc comments with the reflection API (not only return the docComment, but also parse it into appropriate pieces), if this is going to be the "official" way to do this. Note: I'm not making any demands... just an idea/request. S

Andi Gutmans

21 years ago
At 01:33 PM 2/11/2005 -0500, Sean Coates wrote:
>Andi Gutmans wrote: >>I think phpDoc is the solution because especially as we start going into >>the web services realm we're going to need to document more complex >>signatures and this kind of syntax won't be sufficient anyway. >>We will need to have a way to document web services so that we can >>auto-generate WSDL files. > >It would be nice to be able to (optionally) parse phpdoc comments with the >reflection API (not only return the docComment, but also parse it into >appropriate pieces), if this is going to be the "official" way to do this. > >Note: I'm not making any demands... just an idea/request.
Yes, that could be nice. Maybe someone will write an ext/docComment. Shouldn't be too hard. Andi

Greg Beaver

21 years ago
John Coggeshall wrote:
> object and maintain that WSDL alongside code changes manually? WSDL > documents were designed to be generated automatically by the > architecture exposing the web service, and without any notion of typing > in PHP at all there is no viable way to really do this.
Don't forget about the use of in-code documentation to extract type information. I wrote an xml-rpc implementation that relies on reflection and a very small documentation parser to validate incoming signatures. Davey Shafik took some of his own code and the same ideas to write a WSDL generator for his Cerebral Cortex project. The best thing internals could do for WSDL is to add a documentation lexer to reflection, but I would be surprised if this happens - too complicated unless it's really crude :) Greg

Adam Maccabee Trachtenberg

21 years ago
On Thu, 10 Feb 2005, Greg Beaver wrote:
> The best thing internals could do for WSDL is to add a documentation > lexer to reflection, but I would be surprised if this happens - too > complicated unless it's really crude :)
I did something like this using getDocComment() and preg_match() for Services_Ebay. It was really crude. :) -adam
-- adam@trachtenberg.com | http://www.trachtenberg.com author of o'reilly's "upgrading to php 5" and "php cookbook" avoid the holiday rush, buy your copies today!

Andi Gutmans

21 years ago
At 09:08 AM 2/10/2005 +0100, Ante Drnasin wrote: Why not?
>Because it would be a great asset to the new OOP model in PHP 5.... >Because the new OOP model is great and I don't think it shouldn't stop >there.... >Because it would be great if the choice is left to the end user... > >We already have OOP freaks (including this one) and I just love the idea >to have something like > >function Add($a, $b) { > return new Float($a+$b); >} > >print(Add(new Integer(5), new Float(5.7)));
I don't think we should support it just because you're an OOP freak and think it's cool. Actually I see no good reason to do it in PHP. A good reason to have it in Java is because it gives you the option to also set it to NULL. In PHP you don't have that problem. It goes very much against the PHP spirit of dynamic typing and just bloats things. (/me having done a huge project in J2EE a while ago where moving from large composite objects using classes like Integers to native types solved serious scalability problems :) Andi