Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Zend Developer Zone:
A new type of PHP, part 2: Scalar types
Sep 16, 2015 @ 14:09:26

The Zend Developer Zone has posted the second part of their series (from community member Larry Garfield) about scalar types in PHP 7, one of many features in this "coming soon" release. You can find part one of the series here.

In our last installment, we talked about the benefits of more robust variable typing in PHP 7, and specifically the new support for typed return values. That is already a big boon to the maintainability of our code, but PHP 7 goes a step further. So far, we’ve only talked about typing against classes and interfaces. We’ve been able to type against those (and arrays) for years. PHP 7, however, adds the ability to type against scalar values too, such as int, string, and float.

But wait. In PHP, most primitives are interchangeable. [...] Much the same as return types, scalar types offer greater clarity within the language as well as the ability to catch more bugs earlier. That, in turn, can help encourage more robust code in the first place, which benefits everybody.

He starts by looking at the four new types that have been added in PHP 7: int, float, string, and bool. He includes a code example showing each of them in use on class interfaces and functions. He steps through the code example, explaining how the return type checking is handled for each instance. He also talks about how return type hinting can also benefit static analysis tools, allowing them to potentially find bugs in return values easier than before. Finally he covers strict mode, the method for enforcing types in your code and preventing PHP from doing any "magic" type switching for you. He also includes a code example of this functionality and how, with it enabled, it would have caught an error in his example on a integer vs string input.

tagged: scalar type hints introduction php7 strict example

Link: http://devzone.zend.com/6622/a-new-type-of-php-part-2-scalar-types/

Webgeekly.com:
20 Tips you need to learn to become a better PHP Programmer
Dec 14, 2010 @ 18:56:50

If you're one of the developers that have the basics of the language down and want to improve your skills to the next level, you'd do well to check out this post from Webgeekly.com packed with little helpful hints you can use to make your life easier.

PHP is a very versatile programming language that can achieve the same objective in multiple ways. You can read more about that in my 'The Art of Programming' post. Below are a few tips I’ve picked up from past projects that can improve your code readability and maintainability and make you a neater, more organized PHP programmer.

There's lots of useful hints included - most of them are relatively basic, but they can even be helpful as reminders for those seasoned developers:

  • Separate Configuration Files
  • Use Indentation and Spacing
  • Give your Variables Meaningful Names
  • Use Ternary Operators
  • Use Assignment Operators
  • Use Objects instead of Functions
  • Aim for Loose Coupling, Strong Cohesion
tagged: hints tips better programmer opinion

Link:

Johannes Schluter's Blog:
More on scalar type hints in PHP trunk
Nov 23, 2010 @ 15:13:24

Johannes Schluter has posted a bit more information about the scalar type hinting that's been included in the main development line of the PHP language (trunk).

Some time ago I wrote an article about the implementation of type hints for non-object types for PHP. Meanwhile many things happened and that implementation was replaced by a different one. Readers of my previous post might know that I have doubts about type hints in PHP.

He shows some of the example syntax for the hinting and points out how, in one case, there's no error thrown when the type hint is a native one but an error is thrown on a custom type hint.

So why is there a syntax added which is ignored? [...] Well, I let it to you to decide whether it make sense to have two syntaxes which look the same but do very different things (being ignored vs. throwing a terminating error) and whether it makes sense to push a system where the core language behaves differently depending on the system. [...] I seriously hoped PHP was out of the era of introducing new inconsistencies.
tagged: scalar type hints trunk inconsistent

Link:

Mashable:
10 Intermediate and Advanced Tips from PHP Masters
Nov 05, 2010 @ 15:55:36

Following up on their tips for PHP beginners they posted a little while back, Mashable has a a new article with a few more advanced tips from the same group.

Well, if you happen to be one of those more seasoned PHP developers, this post is for you. Our expert panel has returned to offer helpful hints and suggestions for those of you who have long since passed the "beginner" milestone and are on your way to becoming true masters of the art and science of PHP. Feel free to ask questions and leave your own suggestions in the comments — the lovely people we interviewed in this post are quite likely to pop in and reply to you directly.

Tips included in this article are things like:

  • Stay Current
  • Interact With Others' Code
  • Ask Questions
  • Keep It Simple
  • Maintain Your Code
  • Work With Others

Notice a trend? Working with others outside of your own development bubble can really help not only your development skills but can also provide you with a good way to give back to the community.

tagged: intermediate advanced tips development hints

Link:


Trending Topics: