Page 2 of 2
Re: Traits
Posted: Sat Jul 04, 2009 4:51 pm
by Jenk
In other languages they can be used like that.. but why bother with type restriction in a dynamic language?

Re: Traits
Posted: Tue Jul 07, 2009 10:57 am
by Darhazer
Actually it's not a restriction... they call it 'type hinting'

Even in a dynamic language, you expect certain types of the variables
Re: Traits
Posted: Tue Jul 07, 2009 1:07 pm
by Jenk
It's a restriction. No matter how much you sugar coat it, it is still restricting the type parameter.
http://www.google.co.uk/search?q=define
the act of keeping something within specified bounds (by force if necessary); "the restriction of the infection to a focal area"
Re: Traits
Posted: Tue Jul 07, 2009 1:15 pm
by kaisellgren
That's right. There are no ways to go around it, it's a restriction.
Re: Traits
Posted: Tue Jul 07, 2009 3:06 pm
by Darhazer
And why we already have restrictions in PHP (classes, interfaces, and 'array') (offtopic: there was idea to implement type hinting for the returning variables in PHP 6, I'm not sure if it still in the roadmap) but don't restrict ourself with the traits? And is it better to check if method_exist or is_callable in the code?
P.S. Since you are not forced to specify the type of argument and this is just an option, I don't think it's a restriction... It can be restriction in your design... and actually I think it's better to have type-restrictions in the design... of course, following the "Program to an interface, not to an implementation" principle
Re: Traits
Posted: Tue Jul 07, 2009 3:27 pm
by kaisellgren
Darhazer wrote:offtopic: there was idea to implement type hinting for the returning variables in PHP 6, I'm not sure if it still in the roadmap
I'm using PHP 5.3 and I'm (return) type hinting with integers, classes/objects, strings, resources, booleans, arrays and literally with any type there is.
Darhazer wrote:P.S. Since you are not forced to specify the type of argument and this is just an option, I don't think it's a restriction...
I think you have misunderstood type hinting. It does
restrict.
Code: Select all
function test(bool $amihuman) {}
test('5'); // Catchable fatal error: Argument 1 passed to test() must be an instance of boolean, string given, called in
Re: Traits
Posted: Wed Jul 08, 2009 5:49 am
by Darhazer
Thank you for the information, Kai!
What I meant about the type hinting, is that you are not required to enter the expected type... if you do, you are restricting, but PHP itself do not restrict you... nevermind. This do not answer the question if the trait will be considered as a 'type' or 'implemented interface'