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

Evert Pot:
PHP's callable typehint too loose?
May 07, 2015 @ 15:19:56

In his latest post Evert Pot wonders if the current implementation of the "Callable" type in PHP is too loose when it comes to what it will accept as a valid callable resource.

PHP got support for closures in version 5.3, and in PHP 5.4 we got support for a callable typehint. [...] All these little changes make it feel more comfortable to apply functional programming concepts to PHP, but occasionally we need to drop back to using less aesthetically pleasing code.

In his examples of "less aesthetically pleasing code" he shows a few different methods that work that aren't the typical closure or object arguments (like passing in an array of object+method name). He also shows an interesting option where you can use a string with a static method call (ex: "MyClass::method") and it will still be accepted. He points out that for this to work correctly in all situations, the call_user_func method should be used, not just calling the input directly.

tagged: callable typehint loose object method array variable iscallable calluserfunc

Link: http://evertpot.com/on-callables-and-closures/

Paul Jones:
What's The Difference Between Tightly-, Loosely-, and De-Coupled ?
Oct 06, 2014 @ 15:20:30

In his latest post Paul Jones recounts a Twitter-based discussion that happened between Taylor Otwell (@taylorotwell) and others on Twitter about the different types of coupling in libraries or applications. The discussion focused around three different types and their definitions: loosely-coupled, tightly-coupled and de-coupled.

The quotes from the conversation come from Taylor, but Paul includes some of his own thoughts in response (things better expressed in more than 140 characters. He talks about some of the assumptions that were made during the discussion, the general knowledge level of "basic programming terminology" and how Paul views the definition of "decoupled".

If your code has a dependency on classes in a particular thrid-party package, your code is tightly coupled to the code in that package. [...] The fact that your code could be tightly coupled to another package does not mean that the other package is coupled to anything else. That is to say, the other package might have no couplings of any sort to any other code outside itself. The other package in that case is de-coupled.

He talks about how one of the main goals of the packages that make up the Aura project is to be decoupled from the start and how that can help with changing requirements/dependencies down the road. He also defines what he sees as "loose" and "tight" coupling, largely defined by the packages required in the "composer.json".

tagged: decouple loose tight coupling package library definition terminology

Link: http://paul-m-jones.com/archives/6051

Samantha Quinones:
Juggle Chainsaws, Not Types
Nov 22, 2013 @ 15:25:33

Samantha Quinones has a new post today about something that has been known to trip up both new and experienced PHP developers - PHP's dynamic type juggling.

No matter how popular an activity it is, I really don’t like to bash on PHP. Every language has its flaws when you look closely enough, and if PHP wears its idiosyncrasies a little closer to the surface than most, I think it makes up for it in other ways. PHP’s handling of types, however, is confusing at best and at worst completely deranged.

She goes on to talk about the issues with type comparisons and how much trouble using the "==" (double equals) versus the "===" (triple equals) can potentially cause. While it's easier for new PHP developers to get caught by this issue, even experienced devs might miss it. She gives an example of a time in her own development involving the comparison of strings against constants and in_array's non-string type comparisons.

tagged: type juggling strict loose comparison inarray

Link: http://www.tembies.com/2013/11/juggle-chainsaws/

PHPMaster.com:
Introduction to the Law of Demeter
Oct 26, 2012 @ 17:13:35

On PHPMaster.com today they've posted an introduction to the Law of Demeter, an idea that promotes loose coupling between objects that each do their own work. This makes it simpler to make pieces interchangeable and more able to focus on their job.

With so many heuristics stating how and why software systems should cling to a specific approach, it’s pretty disappointing not seeing a broader implementation of them in the world of PHP. For example, the Law of Demeter is probably one of the most underrated in the language’s realm. Effectively, the law’s “talk to your closest friends” mantra still seems to be in a pretty immature state in PHP, something that contributes to rot in the overall quality of several object-oriented code bases.

They look at how keeping the functionality of each object focused and not "knowing too much" is a good thing and include some examples of working with a service locator, serializer and file storage classes.

tagged: lawofdepeter introduction object loose coupling

Link:

Lukas Smith's Blog:
Loose interface coupling
Dec 06, 2011 @ 17:02:18

In a new post to his blog Lukas Smith proposes an idea for a loosely coupled interface setup that would allow for easier integration between third-party libraries and other applications.

Especially as for different libraries a different subset of the community could end up collaborating. Here I see 3 options: 1) each library bundles the interfaces (even though they sit in some common namespace), 2) each project asks their users to fetch the common interfaces from some other place 3) runtime "coupling". Option 3) doesn't exist today and is what this blog post is about.

He introduces the idea of a "spl_register_compatible_interface" method that would let you compare interfaces to see if they'd mesh. There'd still have to be a lot of communication between developers to make things match, though. He suggests three "practical issues" that libraries/tools would have to overcome to use a system like this - each framework has their own interface setup, the lead time for collaboration could be too much to be worthwhile, a lack of interest from some about collaboration and the idea of competing interface methods.

He's looking for feedback from the community on the idea(s) though, so go and leave a comment with your thoughts!

tagged: interface coupling loose opinion collaboration

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:


Trending Topics: