SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: Type safety in PHP

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Dec 2012
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Type safety in PHP

    Hi,
    I am currently working on my final project of my undergraduate study and the project is about type safety in PHP. One aspect of the project examines the opinion of the PHP community on this topic.

    I now need your help. I would be very glad, if you could take 5 – 20 minutes to fill in my online survey: http://www.q-set.co.uk/q-set.php?sCode=PGSKQCJUWZVK

    I will publish the deliverables of my project under an open source license. Thus you contribute automatically to an open source project if you fill in the form

    Thanks for your help
    Robert Stoll

  2. #2
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You may want to read this https://wiki.php.net/rfc/returntypehint2 as the keyword function becomes an alias for return type mixed for backwards compatibility and maybe change your form to represent this as I believe this is the latest rfc even if it is a bit old.

  3. #3
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,518
    Mentioned
    31 Post(s)
    Tagged
    0 Thread(s)
    I find the survey to be... misleading/incomplete.

    You start to say "This will not affect PHP in any way"... and then in the next question propose changing the syntax of the function command.
    So... i cant complete the survey correctly.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  4. #4
    Resident OCD goofball! bronze trophy
    Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    I took your survey, out of curiosity. But I think some discussion could be held here on certain points as well. I realise most of this has been hashed and re-hashed many times over, here, on the PHP discussion groups, and elsewhere on the internet. Though, many of patrons do not visit such places.

    I think the differentiation should be made between type hinting and type safety. The are fundamentally different and likewise, serve different purposes. Type hinting is an as-of-right-now check, while type safety is an at-any-time check. Variables, or return values, marked with a type hint are checked only at the time of entry or return. However, such variables types would still be mutable once the values have been obtained. With type safety, they cannot. Implementing full type hinting would only serve to better error reporting in PHP. Of course, I am not denouncing the impact of this. Rather, I am merely putting it into perspective. As I am primarily a C/C++/C# developer, I would prefer that both aspects be fully utilized, with an ini option to disable it globally, and a method to turn it off for the current script. That said, do I think it is required? No. Part of the charm of PHP is the overall mutability it has. But if done, it needs to be done correctly and completely.

  5. #5
    Hosting Advisor silver trophybronze trophy
    SitePoint Award Recipient cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    2,795
    Mentioned
    44 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Serenarules View Post
    I think the differentiation should be made between type hinting and type safety. The are fundamentally different and likewise, serve different purposes. Type hinting is an as-of-right-now check, while type safety is an at-any-time check. Variables, or return values, marked with a type hint are checked only at the time of entry or return. However, such variables types would still be mutable once the values have been obtained. With type safety, they cannot. Implementing full type hinting would only serve to better error reporting in PHP. Of course, I am not denouncing the impact of this. Rather, I am merely putting it into perspective. As I am primarily a C/C++/C# developer, I would prefer that both aspects be fully utilized, with an ini option to disable it globally, and a method to turn it off for the current script. That said, do I think it is required? No. Part of the charm of PHP is the overall mutability it has. But if done, it needs to be done correctly and completely.
    I agree with that. However, I also think that full type safety would be hard to implement in PHP since it isn't really a compiled language. Granted I guess once you start to serve up the page the JIT could find the errors and report them, but if you ask me, that is too late (from a developer perspective). That is one reason I really enjoy C/C#, I love the instant notification I can get by simply compiling my code.

    Also, granted, several IDEs are very capable of detecting these errors on your behalf before your code is asked to be rendered/compiled too, so you could argue it could still be implemented on that level too. But I digress, if your IDE did that, do you really need full type safety? Wouldn't type hinting with IDE support for detecting errors be enough? The only thing I dislike about that idea, is you are separating responsibility, PHP is responsible for continuing type hinting, and IDEs are responsible for ensuring your code sends appropriate values to your type hinted methods, classes, etc. -- Seems very dirty.

  6. #6
    Resident OCD goofball! bronze trophy
    Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    I agree with that. However, I also think that full type safety would be hard to implement in PHP since it isn't really a compiled language. Granted I guess once you start to serve up the page the JIT could find the errors and report them, but if you ask me, that is too late (from a developer perspective). That is one reason I really enjoy C/C#, I love the instant notification I can get by simply compiling my code.

    Also, granted, several IDEs are very capable of detecting these errors on your behalf before your code is asked to be rendered/compiled too, so you could argue it could still be implemented on that level too. But I digress, if your IDE did that, do you really need full type safety? Wouldn't type hinting with IDE support for detecting errors be enough? The only thing I dislike about that idea, is you are separating responsibility, PHP is responsible for continuing type hinting, and IDEs are responsible for ensuring your code sends appropriate values to your type hinted methods, classes, etc. -- Seems very dirty.
    Exactly. Which is why I, overall, opt for the 'not required' option. And you're right. The way PHP handles certain scalars is not ameniable at the time for implenting type hints: string, int, etc).

    Anyway, you bring home a point most people seem to miss. Compiled vs Interpreted. In addition, the original intent of the given language. PHP was designed to be fast, light-weight and mutable. While it has seen more use lately in larger projects (business driven distributed applications), that isn't what it was designed for originally. The language is progressing, as more and more web sites make use of advanced concepts, but it's going to be a long road.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •