On 09/10/12 19:20, Jazzer Dane wrote:
- "If we ever get return type hinting/checks then we needn't consider how
the syntax has to look" From what I know, this isn't planned for PHP 5.5
and any proposals for it have been largely ignored. Return type hinting
won't help when setting either, although it would help with getting. All
that being said, type hinting aside, the syntax I proposed is, in my
opinion, the most consistent out of any other proposal thus far (arguably
aside from yours, I'll go over that momentarily).

Excellent point. Return type-hints only affect the getter, and even then it could be a moot point if the property itself could be bound to a particular type. It would actually be a much cleaner solution, and wouldn't require any superfluous parens in the declaration:

class MyClass{

    public SomeClass $property {
        get {...}
        set {...}
    }
}

$foo = new MyClass;
$bar = new SomeClass;
$foo->property = $bar; //ok
$foo->property = new StdClass; //type mismatch

Heck, half the time setters and getters are implemented specifically for this purpose. If the property itself could be typed, then 90% of the time we wouldn't even need to specify get/set. Just give the property a type and be done with it.

That said, it wouldn't help for scalar values, and that leads to a problem with the current syntax (AFAICS). You can't specify the initial value for a property:

eg:
class MyClass{

    public $property = 5    //?can we do this?
    {
        set($value){
            if(!is_int($value)){
                throw new InvalidArgumentException('value of wrong type');
            }
            $this->__property = $value;
        }
    }
}

Or would that have to happen in the constructor?

class MyClass{

    public $property
    {
        set($value){
            if(!is_int($value)){
                throw new InvalidArgumentException('value of wrong type');
            }
            $this->__property = $value;
        }
    }

    public function __construct(){
        $this->property = 5;
    }
}


Cheers,
David

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

Reply via email to