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

SitePoint PHP Blog:
Creating Strictly Typed Arrays and Collections in PHP
Mar 27, 2017 @ 17:45:06

On the SitePoint PHP blog there's a new post from Bert Ramakers showing you how to create strictly typed arrays and collections in PHP.

One of the language features announced back in PHP 5.6 was the addition of the ... token to denote that a function or method accepts a variable length of arguments.

Something I rarely see mentioned is that it’s possible to combine this feature with type hints to essentially create typed arrays.

He starts with an example of a class/method that only takes in a certain type of objects as a collection (using the "...") with a fatal thrown if anything else is given. He also shows how to do the same thing with scalar types and the "..." operator with a typed input. He does point out one problem with this approach, namely that if more complex input is required the single type just wouldn't work. His solution involves custom collection classes where the settings are in the collection and not passed directly into the method. This collection then contains some of the base functionality (like getting an average value from a set of floats) and can be enhanced with other typical interfaces to work like any other collection. He also presents another option: using value objects for validation of the input.

tagged: collection array strict typing tutorial operator

Link: https://www.sitepoint.com/creating-strictly-typed-arrays-collections-php/

StartTutorial.com:
5 New Features In PHP 7 That You Should Have A Look At
Oct 19, 2016 @ 14:43:05

If you've heard about the release of PHP 7 but aren't quite sure what it has to offer, check out this quick post on the StartTutorial site giving you a "top five" list of things this new version of the language has to offer.

But you must be wondering why PHP named its latest release PHP 7 and not PHP 6. Reason behind it is that, many of the PHP 6 releases were already implemented in PHP 5.3 and later, there was not really a proper reason just to change the name. What I am trying to say here is that we haven’t missed anything. Just to avoid the confusion with a dead project, PHP's latest release was named to PHP 7.

Is This Hype Valid for PHP 7? What It Actually Brings Forth for the Developers? Hop on and let’s take a deeper dive. Let's check out what new features PHP 7 has to offer. And what improvements those features will bring forth.

Their top five list covers some of the major improvements in the language:

  • Speed Improvement
  • Implementation of Type Declarations
  • Implementation of Error Handling
  • New Operators
  • CSPRNG Functions

There's a bit of explanation of each item on the list but you'll definitely want to refer to the PHP manual for more details and specifics on what changed in PHP 7.

tagged: php7 feature top5 list speed typing errors operators csprng

Link: https://www.startutorial.com/articles/view/5-new-features-in-php-7-that-you-should-have-a-look-at

Three Devs and a Maybe:
Episode #98 - Instant Feedback on your Pizza Oven
May 19, 2016 @ 14:20:35

The Three Devs and a Maybe podcast has released their latest episode today - Episode #98: Instant Feedback on your Pizza Oven, a discussion on quick feedback cycles, MVCC in Databases and Type Systems

On this weeks episode Edd and Mick start off discussion around productivity, quick feedback cycles with clients and building pizza ovens. We then move on to chat about how Edd ‘maybe’ accident prone, how MVCC works at a high-level and why you need to vacuum your tables in Postgres. Finally, we bring up Uncle Bob’s recent ‘Type Wars’ article and highlight the different variants of typing (static, dynamic, weak and strong).

You can listen to this latest episode either using the in-page audio player or by downloading the mp3 directly and enjoying at your leisure. If you enjoy the show, be sure to subscribe to their feed and get the latest updates on new shows as they're released.

tagged: threedevsandamaybe podcast ep98 feedback mvvc database typing pizzaoven

Link: http://threedevsandamaybe.com/instant-feedback-on-your-pizza-oven/

Acquia Blog:
Quick Tips for Writing Object Oriented Code in PHP
Jul 13, 2015 @ 15:58:14

On the Acquia blog Adam Weingarten has shared some tips for writing good (and modern) object-oriented code in PHP:

Recently I began working on a D8 module, but this isn't a story about a D8 module. The work I did provided me an opportunity to get back to my pre-Drupal object oriented (OO) roots. Writing OO code in PHP presented some curve balls I wasn’t prepared for. Here are some of the issues I encountered:

His tips touch on things like:

  • Using a code structure that can be autoloaded via PSR-4
  • Namespacing your classes
  • Working with types and type hinting
  • Using docblock comments for autocomplete in IDEs

There's also a few other quick topics he finishes the post out with: the lack of enums in PHP, working with associative arrays, no functional overloading and assigning responsibility to classes.

tagged: oop tips objectoriented code modern psr4 namespace typing docblock missing

Link: https://www.acquia.com/blog/quick-tips-for-writing-object-oriented-code-in-php/09/07/2015/3285651

Allan MacGregor:
Hello Hack
Mar 25, 2014 @ 14:50:33

Allan MacGregor has a new post to his site introducing you to Hack, the recently released language from Facebook based on their work with the HipHop virtual machine.

Recently Facebook unveiled Hack, a new programming language that aims to provide developers with the tools to write and ship code quickly while catching errors on the fly. Hack is as programming language designed to work with HHVM and the same time it works seamlessly with PHP as Facebook lead devs. [...] So does that mean that Hack is just a faster more efficient PHP implementation, right? Wrong, Hack is much more than that, the language brings features that are normally found in statically typed languages to the dynamically typed world of PHP.

He focuses in on the static typing functionality of Hack a bit more and includes an example of PHP code where Hack would catch the type shifting. He also briefly mentions some of the other features including built-in collections, traits, generics and asynchronous programming.

tagged: hack facebook language introduction typing

Link: http://coderoncode.com/2014/03/23/hello-hack.html

DZone.com:
The Duck is a Lie
Jun 27, 2012 @ 14:55:22

In this recent post to DZone.com Giorgio Sironi looks at duck typing and compares it in a few different languages (PHP, Ruby and Java). "Duck typing" is where the methods/functions define the structure or common interface rather than being functional.

What follows is my experience with Java, PHP and Ruby. I mainly use PHP as a dynamic language that supports duck typing but also the definition of Java-like interfaces, but does not force any of the two approaches as you can define interfaces whose method arguments accept any variable or not using interfaces at all. Is duck typing that a revolution?

He shares some of the common misconceptions he's seen including the idea that duck typing can help completely different objects work together and that, sometimes, despite naming conventions, functionality was intended to be different. He shows how even interfaces in PHP can be implemented loosely and the "acts as" and "single callback" architecture ideas.

tagged: duck typing interface misconception actsas callback

Link:

Pim Elshoff's Blog:
In favour of typing
Apr 25, 2012 @ 18:57:38

Pim Elshoff has a new post to his blog that shares his preference on typing (keystrokes, not variables) in applications (hint: he likes it):

We sometimes conceive of ideas that are arduous to express in code. Like persisting data, or some other uncommon task (sarcasm). It's not difficult, but it takes a lot of keystrokes to write. Being problem solvers, we find it difficult doing this kind of manual labour, especially when machines can do it for us. Still, I would like to take this opportunity to say that typing rocks and solutions that save typing suck.

He talks about the abstraction that frameworks provide (less typing, more work) and and some of the "magic" that comes with them. He gives specific examples of some of his pervious experience with frameworks (including some pains with Symfony2) and how some of the magic he's seen is easy to write but hard to read.

tagged: favor typing keystrokes framework magic opinion

Link:

Evert Pot's Blog:
Numeric string comparison in PHP
Apr 26, 2011 @ 14:23:47

In this new post to his blog Evert Pot reminds you (the PHP developer) about the loose and strict typing checks that you need to do when checking the values of your variables and never just assume. PHP's loose typing tendencies can cause issues if you're not paying attention.

As long as you make sure you always use strict checking (=== and !==) where you can, and fall back to the loose checks when you must. As a PHP developer, I think it's very important to understand and memorize exactly how these work, whether you're writing new code, or maintaining existing code.

He points out a specific example of some code that seems counter-intuitive when you compare how it reads and how it actually works (strings converted to floats and evaluated)

The moral is: always do strict checks when you are able to.
tagged: numeric comparison evaluate loose typing typecheck

Link:

Giorgio Sironi's Blog:
Java versus PHP
Apr 16, 2010 @ 15:55:19

In a new post to his blog Giorgio Sironi has a (more balanced) comparison of Java versus PHP that them on topics like how they handle typing and their execution models.

If you exclude C and its child C++, the most popular programming languages in the world are Java and PHP, which power most of the dynamic web. I have working experience with PHP and for academical purposes I am deepening my knowledge of Java, thus I'd like to point out similarities and key differences between these two languages. Every language has its pros and cons, so there's no absolute winner here.

He briefly covers their history, java's static versus PHP's dynamic typing, how they both handle objects, their execution plans and how easy they are to deploy when it comes to general applications.

tagged: java comparison typing oop execution infrastructure

Link:

Jani Hartikainen's Blog:
Static/Dynamic typing sweet spot
Oct 08, 2008 @ 15:24:28

In a new post today Jani Hartikainen takes a look at one of the things that makes PHP both powerful and unpredictable at times - its variable typing, static versus dynamic.

It seems that PHP has moved a bit towards static typing, and languages like C# seem to be implementing some dynamic features. Are we going towards a "mixed" language with static and dynamic typing? What's the "sweet spot" between completely static (like C++) and completely dynamic typing (like Python)?

He compares the benefits of static and dynamic, and a particular place where PHP falls a bit flat - type hinting in certain situations. He agrees, though, that dynamic typing is "the way to go" since it gives the developer the flexibility they might need for more "typing tricks" in their apps. Oh, and "duck typing"...

tagged: static dynamic typing type variable hint compare

Link:


Trending Topics: