Traits

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: Traits

Post by Jenk »

In other languages they can be used like that.. but why bother with type restriction in a dynamic language? ;)
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Traits

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: Traits

Post 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"
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Traits

Post by kaisellgren »

That's right. There are no ways to go around it, it's a restriction.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Traits

Post 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
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Traits

Post 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
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Traits

Post 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'
Post Reply