Hi,

Not quite. The proposed is a syntactic sugar which is thought to handle any
transformation of a value, not necessarily or limited to type or class
conversion. It is of course possible to limit the usage to just that, with
any user defined convention or "best practice". In fact it's pretty
distinct from primitive casting, I just had in mind that reusing the
casting syntax could be an advantage due to similarity of the behavior.

In simple words the statements of *$var = (ClassName)$var* or
*function(ClassName
$var){}* would not be read as "Cast to", but "Cast with".

The example suggestion with wrapping the value in an object just for
handling value validation/sanitization is not just overkill, but also is
excess, since there is no any need to have the value wrapped after the
function input processing. In fact, the closest construct to the mentioned
is:

function foo(/* to be casted with PositiveInteger */ $i) {
    $i = PositiveInteger::cast($i);
    return 2*sqrt($i);
}

Hope this makes sense.

On Sun, May 13, 2012 at 7:40 PM, Stas Malyshev <smalys...@sugarcrm.com>wrote:

> Hi!
>
> > Please be kind to review and comment my proposal for custom casting in
> PHP
> > (or let me know if a similar proposal was already discussed).
> >
> > http://pastebin.com/sPb0i8U6
>
> I think there's an issue with this idea. As far as I understand, what is
> proposed is kind of cast-constructor paradigm. However, the result of
> (ClassName)$var would be expected to be an object of type ClassName.
> That is, however, not what cast() returns - it returns integer $var.
> Since there's no way for foo(ClassName $var) to know where $var came
> from, the result of $var = (ClassName)$var; is just a simple integer and
> foo() would reject it since it's not an object of type ClassName.
>
> Of course, you could just create this instead:
>
> class PositiveInteger {
>  private $i;
>  function __construct($i) {
>     if(intval($i) < 1) $i = 1;
>     $this->i = $i;
>  }
>  function intval() {
>     return $this->i;
>  }
> }
>
> function foo(PositiveInteger $i) { return 2*sqrt($i->intval()); }
>
> and then:
>
> foo(new PositiveInteger(42));
>
> But this is doable right now, even if sounds a bit of overkill for me. I
> wouldn't mind also having (int)$i done with some magic methods, as
> proposed in RFCs you mentioned.
>
> --
> Stanislav Malyshev, Software Architect
> SugarCRM: http://www.sugarcrm.com/
> (408)454-6900 ext. 227
>

Reply via email to