I'm essentially trying to emulate the .NET style boxing/unboxing
functionality natively in PHP while maintaining strict type checking and not
affecting bc or performance (as part of a larger project converting the .NET
BFL to a php extension). I already have a large component of it working in
userland classes which uses a ZConvert class that calls the IComparable
interface of the classes (same as .NET). Using PConvert::ToBoolean($ZBool)
everywhere is the nicest solution. 

In .NET all boxable types descend from a ValueType class. The optimal
solution would be to have zend understand a IValueType interface and perform
the boxing/unboxing (probably using the cast_object handler or accessing a
ce->property value which is how .NET handles it internally) as necessary.
This would handle the unboxing for the majority of cases I think, so you
could even pass IValueTypes to standard php functions (which parse their
params using the convert_to_* functions.

final class ZString extends PValueType implements IValueType { ... }
final class ZInt32 extends PValueType implements IValueType { ... }
... etc for the native PHP types

$zstring = new ZString("PHP "); // $zstring = (object)"PHP ";
$zdouble = new ZDouble(5.1); // or possibly even  $zint = (object)5.1;

$zstring .= $zdouble;
$zdouble *= 2;

$int = strlen($zstring); // goes through zend_parse_parameters which
understands an IValueType object.

Where $zstring and $zdouble remain their respective object types.

And then going the other way... if it was possible for an extension to
provide a callback to the (object) ZEND_CAST then the following would be
possible (the closest I think it could get to real boxing)

Note the TypeHints forcing a true object interface, then the explicit cast
in the function call.

final class ZString extends ZValueType implements IValueType
{
        public static function Concat(ZString $strA, ZString $strB)
        { 
                return new ZString($strA.$strB); 
        }
}

$zstring = new ZString("PHP ");
$zstring = ZString::Concat($zstring, (object)"5.1.0");


The callback would determine the object type being cast and create a new
ZValueType of that object type.

I think my thoughts got all jumbled describing this...I know the unboxing of
the types will work with no changes....its just the boxing that would
require changes by registering a callback during object casts. Like I said,
this is mainly for the experience right now, but I would like to release the
.NET BFL extension as some point in time. (Along with the ASP page/component
processing model) How cool would it be to develop PHP web pages visually in
Visual Studio? I'll tackle that part of it when I get this part done :)

Bob Silva

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to