News Feed
Sections




News Archive
feed this:

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

Matthias Noback:
Composer "provide" and dependency inversion
October 06, 2014 @ 09:53:20

Matthias Noback has a new post today responding to a recent post talking about virtual packages with Composer (using "provide") and some of his own thoughts of how it relates to dependency inversion.

This is a response to Peter Petermann's article Composer and virtual packages. First, let's make this totally clear: I don't want to start an Internet war about this, I'm just pointing out some design issues that may arise from using Composer's provide option in your package's composer.json file. [...] Yes, if a user wants to run the code in your library, they need to have some class that implements [the "provides" requirement]. But no, this shouldn't be reflected in the dependencies of the library. Let me explain this by taking a look at the Dependency inversion principle.

He gives an example of using a specific package for logging (the Zend logger) and how that hard-coded dependency can be refactored out using one of two methods: either a custom interface or one described elsewhere. Getting back to "provide", he lists some reasons why he thinks that defining the interface itself in the Composer configuration is a good idea. These include:

  • Strictly speaking (as in, would the code compile), the code from the library itself [...] just needs the LoggerInterface (which happens to be in the psr/log package).
  • By depending on an implementation package, you basically undo any effort you made to depend on abstractions and not on concretions.
  • Some day, someone may decide to introduce another virtual package, called the-real-psr/log-implementation.
  • The notion of an "implementation package" is really vague. What does it mean for a package to be an implementation package.

Each of the reasons has a bit of description to go along with it. He also points out an interesting example where the package actually knows about existing virtual package, the DoctrinePHPCRBundle and its use of "jackalope" and "phpcr".

0 comments voice your opinion now!
composer dependency inversion provide configuration interface

Link: http://php-and-symfony.matthiasnoback.nl/2014/10/composer-provide-and-dependency-inversion/

Matthieu Napoli:
Decoupling packages
September 26, 2014 @ 13:42:24

In a recent post to his site Matthieu Napoli looks at some first steps you can take to help decouple packages in your application. He describes a few considerations and methods to think about as you try to break those chains.

Decoupling packages is a hard thing. There are not a lot of options, and this blog post is about how some options are better than others.

Let's say for example that you are writing a "package", or library, to respond to HTTP requests (that kind of package could be considered the basis for a web framework). How do you handle routing? If you write your Router package as an independent package (which is good: small and specialized packages are more reusable and maintainable), you might not want to couple the HTTP package to the Router package: you want to leave users free to choose the router of their choice. So, what are your options to make the HTTP package and the Router package decoupled from each other?

He looks at a few different approaches including focusing on event-driven programming or splitting things along "edges" and making interfaces/adapters to hook them together. He also puts an emphasis on standardizing interfaces, even those outside of your own internal to the application (think the set of PHP PSRs).

0 comments voice your opinion now!
decouple package event interface adapter standardized

Link: http://mnapoli.fr/decoupling-packages/

Marc Morera:
Bye Bye Symfony
September 04, 2014 @ 12:41:00

In his most recent post Marc Morera says bye bye to Symfony and "hello to PHP". Confused? His point is that using the Symfony components as a whole in the framework isn't the only option anymore. You can use them just as effectively as pieces of a larger PHP project, puling them in as needed.

The reason of this post is just to tell you, with a simple example, how to say Bye Bye, Symfony! and say Hi PHP!. This really means uncouple from Symfony Components and still use them as the default implementation, while we can securely remove, from the composer require block, our Symfony dependencies.

He starts off with a simple example showing how to use Symfony's "UrlGeneratorInterface" to create a URL output class that can be injected to use in the route handling of the application. He then moves on to a more real-life example (a metaphor) using a USB connection and the adapters/cables that could be involved to connect various devices. He then shifts back over to the world of code and describes a specification interface that can be used with the URL generation and remove the Symfony dependency from it. On top of this he builds an adapter object that brings the Symfony component back into the picture and abstracts it out a level to make for more flexibility and testability in the long run.

We win maximum implementation flexibility and minimum coupling. Would be wise to say that a PHP project should tend to this thought, but once again, it depends on many factors. [...] Using ports and adapters is really a great tool for those who want to uncouple from implementations and a great pattern if you develop open source. Open source should satisfy as people as possible, so remember, specify and then implement.
0 comments voice your opinion now!
symfony component abstract tutorial interface specification

Link: http://mmoreram.com/blog/2014/09/01/bye-bye-symfony/

SitePoint PHP Blog:
How to Use the JsonSerializable Interface
May 05, 2014 @ 11:50:28

Matrin Hardy has a new tutorial posted to the SitePoint PHP blog today showing you how to use the JsonSerializable interface to work with objects and converting them to JSON.

Over the past few years JSON has taken over as the king of data interchange formats. Before JSON, XML ruled the roost. It was great at modeling complex data but it is difficult to parse and is very verbose. [...] I think we could all agree that writing less code that in turn requires less maintenance and introduces less bugs is a goal we would all like to achieve. In this post, I'd like to introduce you to a little known interface that was introduced in PHP 5.4.0 called JsonSerializable.

He splits the rest of the post out into three different parts: the "ugly" method of converting a sample Customer object into a JSON string (through an array), the "bad" method using a "toJson" method and finally the "good", implementing a class that implements the JsonSerializable interface for easy JSON-ification.

0 comments voice your opinion now!
jsonserializable interface tutorial introduction beginner

Link: http://www.sitepoint.com/use-jsonserializable-interface/

SitePoint PHP Blog:
Using Solarium with SOLR for Search - Setup
May 02, 2014 @ 11:49:16

The SitePoint PHP blog has posted a tutorial showing you how to use the Solarium library to search SOLR. Solarium is a PHP-based, open source tool that helps make interfacing with a SOLR search instance much easier. This post is part one of a larger series covering the combination of SOLR and Solarium.

Apache's SOLR is an enterprise-level search platform based on Apache Lucene. It provides a powerful full-text search along with advanced features such as faceted search, result highlighting and geospatial search. [...] If you're using PHP then the Solarium Project makes integration even easier, providing a level of abstraction over the underlying requests which enables you to use SOLR as if it were a native implementation running within your application. In this series, I'm going to introduce both SOLR and Solarium side-by-side.

He starts with some of the basic concepts behind what SOLR is, what kinds of things it's useful for and how to get it installed on your system (using Homebrew). He shows how to set up a sample schema including a detailed look at the different types and required fields it will need. As this is just the first part of the series, it stops there and will get into the actual PHP code for the interface in the next edition.

0 comments voice your opinion now!
solr solarium search engine tutorial interface opensource library

Link: http://www.sitepoint.com/using-solarium-solr-search-setup/

Suresh Ariya:
Implement Flash Messages using Session in PHP (Part 1 & 2)
February 06, 2014 @ 11:08:55

In a two part series on his site Suresh Ariya shows you how to implement "flash messages" in your applications (in a framework-agnostic way) with the help of a custom script and the current user's session.

As part of the post series, today we are going to see how we can implement Flash Messages using PHP Session. Before proceeding into that, first i like to explain what is Flash Message and its usage. [...] Flash message is a message that will be shown/displayed only once. if you reload the browser or navigated to other pages and came back, you won't see the same message displayed again.

In part one he introduces the concepts behind flash messaging and gets into the initial steps of the implementation via a "FlashMessageInterface" to define the structure. In part two he gets into the actual implementation and shares a script that uses a custom prefix to define the messages and the expected getter/setter methods as well as "clear" functionality.

0 comments voice your opinion now!
flash message series part1 part2 implementation interface

Link: http://sureshdotariya.blogspot.in/2013/12/implement-flash-messages-using-session.html

NetTuts.com:
SOLID Part 3 - Liskov Substitution & Interface Segregation Principles
January 27, 2014 @ 11:51:30

On NetTuts.com today they've continued their series covering the SOLID development principles with the next letter in the acronym - "L". It stands for the Liskov Substitution & Interface Segregation Principles. The tutorial also talks some about the "Interface Segregation Principle" as they go hand-in-hand.

The concept of this principle was introduced by Barbara Liskov in a 1987 conference keynote and later published in a paper together with Jannette Wing in 1994. Their original definition is as follows: "Let q(x) be a property provable about objects x of type T. Then q(y) should be provable for objects y of type S where S is a subtype of T." [or more simply] "Subtypes must be substitutable for their base types."

They include some example PHP code showing a base "Vehicle" class and first an example of doing it correctly (with the Template design pattern) and an example of an incorrect method, complete with tests. They then get into the Interface Segregation Principle, an interface that can be depended on to use the module, with the same car-related examples.

0 comments voice your opinion now!
solid design principles liskov substitution interface segregation

Link: http://net.tutsplus.com/tutorials/php/solid-part-3-liskov-substitution-interface-segregation-principles/

SitePoint PHP Blog:
Fun with Array Interfaces
December 09, 2013 @ 10:53:06

On the SitePoint PHP Blog a tutorial has been posted recently about having some fun with array interfaces via some of the functionality provided through the SPL (Standard PHP Library).

As a programmer who works with different languages every day, I find a lot of joy in learning how things are done differently in other languages and seeing if I can do the same in PHP. One thing I liked in particular in Python was how one can emulate features of native data types in custom classes. [...] I thought it would be nice if you could do the same in PHP on an instance of your custom classes and not only arrays. PHP lets us do this with array interfaces.

He illustrates his intent with some basic Python functionality and shows how to use various PHP interfaces to achieve a similar functionality. He talks about SPL interfaces like Countable, ArrayAccess and Iterator to make objects more useful in an array handling environment. His example uses the idea of a set of user's tweets (from Twitter) and shows the implementation of these three interfaces.

0 comments voice your opinion now!
array interface countable arrayaccess iterator tutorial

Link: http://www.sitepoint.com/fun-array-interfaces

Russell Walker:
Active Record vs Data Mapper for Persistence
October 18, 2013 @ 10:19:13

Russell Walker has a new post today comparing two popular methods for abstracting out database access and working with your data - the Active Record and Data Mapper patterns for data persistence.

These two design patterns are explained in Martin Fowler's book 'Patterns of Enterprise Application Architecture', and represent ways of handling data persistence in object oriented programming.

He gives simple code examples of both - one showing a basic "save" call with Active Record and the other showing the saving of a "Foo" entity using similar logic. Along with these examples, he also includes a few points about the major advantages and disadvantages related to the pattern. He also talks some about "service objects", the go-between that the data mapper pattern uses to combine business logic and the mapper object. He ends the post by making some suggestions about which to use, depending on the need of course.

0 comments voice your opinion now!
activerecord datamapper persistence database interface designpattern

Link: http://russellscottwalker.blogspot.co.uk/2013/10/active-record-vs-data-mapper.html

Procurios Tech Blog:
Autocompleting a lot of parameters
October 16, 2013 @ 11:14:05

Pim Elshoff has a recent post on the Procurios tech blog looking at autocompletion on function calls and an alternative to the "too many parameters" problem.

Some methods have many parameters. Sometimes they start out like that, sometimes they grow like that over time. Even though a maximum of two parameters is preferable, configuration for a method that does a big thing is difficult. Take curl for example; curl has a lot of options and so several wrappers around curl have arisen to deal with configuring it in a more humane manner. How can we keep the clutter of many parameters as low as possible, while maintaining autocompletion?

He gives an example of a function that takes too many arguments and how it's difficult to read (and remember the right order/types to give). He does mention one way that's sometimes used - arrays - but you lose typing checks with that. His best recommendation is to use a fluent interface instead. Not only does it make it more readable but it also works with the autocompletion in most IDEs.

0 comments voice your opinion now!
autocomplete parameters suggestion array fluent interface

Link: http://tech.procurios.nl/archief/2013/10/11/Autocompleting-a-lot-of-parameters


Community Events





Don't see your event here?
Let us know!


security series voicesoftheelephpant mvc tool interview package laravel symfony library version podcast release community framework composer update opinion language introduction

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework