Traits
Moderator: General Moderators
Re: Traits
In other languages they can be used like that.. but why bother with type restriction in a dynamic language? 
Re: Traits
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
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
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"
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Re: Traits
That's right. There are no ways to go around it, it's a restriction.
Re: Traits
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
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
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Re: Traits
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: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 think you have misunderstood type hinting. It does restrict.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...
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 inRe: Traits
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'
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'