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

Mathias Verraes:
Resolving Feature Envy in the Domain
August 12, 2014 @ 11:55:24

Mathias Verraes has a new post today about something he calls "feature envy" in the domain, related to this code smell (based on a definition from Martin Fowler).

Benjamin Eberlei did a really nice job of explaining refactoring the Feature Envy code smell on his blog. I wrote a comment because I felt the example could be taken one step further. You should read the original post. Below are Benjamin's code examples (for reference), followed by a repost of my comment.

The "smell" is defined as "a method that seems more interested in a class other than the one it's in". Mathias includes the code examples from the other post showing a datetime calculation and how it could be abstracted out to another class and method. He talks about the migration and how it relates to the "Whole Value" pattern and integrating some of the logic into a factory, generating a "reporting period" instance. He finishes the post with a brief look at an application of domain-driven design concepts to the problem, suggesting that the reporting be even more abstracted from the datetime data and using the "reporting period" object instead.

0 comments voice your opinion now!
feature envy whole value designpattern class object abstraction domaindriven

Link: http://verraes.net/2014/08/resolving-feature-envy-in-the-domain/

SitePoint PHP Blog:
Repository Design Pattern Demystified
June 02, 2014 @ 09:12:40

The SitePoint PHP blog has a new post today that hopes to "demystify" the Repository design pattern with an overview of its structure and ow your code can put it to use.

What is the Repository Design Pattern? To put it simply, it is an implementation of a brokering layer between the application and a data source. Neither party needs to be be aware of the other to perform their respective jobs which allows us to have a decoupled architecture which in turn helps in the scaling of the application in the big leagues without having hard dependencies.

He includes an example case where the Repository pattern might be used, to "proxy" requests to multiple types of data sources from many different inventory systems. He includes some pros and cons of using the functionality too, like a positive separation of concerns but a negative additional abstraction layer. The remainder of the post includes a code example basing it on a Laravel framework installation. He creates a simple Repository class and shows how to extend it with a layer specific to one city type.

0 comments voice your opinion now!
repository designpattern introduction tutorial laravel class

Link: http://www.sitepoint.com/repository-design-pattern-demystified/

Master Zend Framework:
Creating Custom ZFTool Diagnostic Classes
May 21, 2014 @ 11:23:59

Continuing on from his previous post introducing you to the ZFTool for Zend Framework 2 applications, Matthew Setter has posted part two of the series focusing on the creation of custom diagnostic classes for the tool.

In this week's tutorial, we're going to see how to step beyond the in-built classes and write our own custom checks. Specifically, we're going to write a check which runs php lint on the module's config file, module.config.php. The reason for doing this is because this file is so important in the configuration of a ZF2 module, that we should have a helpful sanity check for it.

He starts by helping you get all the needed dependencies in place, the ZFTool and ZendDiagnostics modules, installed via Composer. He includes code to help get started on the new diagnostic class and accompanying files. He implements some required methods from an interface, and shows how to enable its checking and define the configuration file. He includes a screenshot of the output so you can ensure things are working as they should be.

0 comments voice your opinion now!
zendframework2 zftool custom diagnostic class tutorial

Link: http://www.masterzendframework.com/zftool-2/creating-a-custom-zftool-diagnostic-class

Stephan Hochdörfer:
Controller classes vs. Action classes
May 19, 2014 @ 10:52:31

In this new post to his site Stephan Hochdörfer covers some of his own thoughts about the recently proposed application structure from Paul Jones, the "Action-Domain-Response" pattern. In this post Stephan compares the typical controller classes with an action class.

First of all I do have the feeling that controller classes make it harder to structure your logic. I have seen a lot of "God Controllers" that do a shitload of stuff. Stuff that is not really related to each other. [...] action classes tend to be rather small, typically less than 100 loc for us. That also helps a lot when trying to understand what`s going on. I am aware that there are developers out there who are afraid when it comes to dealing with a lot of classes. [...] That`s another bonus point for action classes: It is easier to search for a class name than a method name in most IDEs.

He goes on to talk more about "God controller" classes, their dependencies and how that compares to action classes only taking in what they need. He touches on the reusability of action classes as opposed to controllers and how they come in handy for storing common logic.

0 comments voice your opinion now!
controller action class compare logic dependency

Link: http://blog.bitexpert.de/blog/controller-classes-vs.-action-classes

Mathias Verraes:
Final Classes
May 13, 2014 @ 09:48:43

Mathias Verraes has posted some of his thoughts about using "final" in classes and what kind of impression it gives about your code.

I make all my classes final by default. I even configured the templates in my IDE prefix new classes with 'final'. I've often explained my reasoning to people. A blog post is in order! A guiding principle here is Clarity of Intent. [...] The reason we need clean code, is not for the compiler. It's to help our fellow developers, third parties, and even ourselves in six months time, understand the purpose and the design of our system.

He relates this concept of clean code and clarity back to the SOLID development principles, specifically the "Open/Closed Principle". This principle states that software should be open for extension but not for modification. It suggests that providing a stable, known API is a responsibility of the developer and using things like callbacks and listeners is a better way to extend. He gets into a bit more PHP-specific issues around using "final", including the difficulties that it can cause during testing.

0 comments voice your opinion now!
final class inheritance extension solid openclosed principle

Link: http://verraes.net/2014/05/final-classes-in-php/

VG Tech:
Comparing Your Privates in PHP
March 19, 2014 @ 09:56:33

In a new post to their blog, the VG Tech folks talk about "comparing your privates" with a "hidden" feature of PHP. Don't worry, they're referring to private class properties on object instances here...

I was going to compare several private properties between to objects and started making a piece of code to perform the actual comparison using getters for the properties. I felt the approach sucked, and started looking into alternatives way to do this.

He shares what the current PHP documentation shares about comparing objects, but neither of them take private properties into account. He remembers, however, that object visibility is at the class level not instance level, allowing two object instances of the same class to have access to all properties of the other, regardless of exposure level. He includes a code snippet showing how to use this to compare those private properties.

0 comments voice your opinion now!
private comparison object instance class

Link: http://tech.vg.no/2014/03/14/comparing-your-privates-in-php/

Gonzalo Ayuso:
Auto injecting dependencies in PHP objects
March 05, 2014 @ 09:19:38

In his latest post Gonzalo Ayuso shows how you can automatically inject dependencies into your PHP objects with the help of Pimple, a simple dependency injection container.

I must admit I don't really know what's the correct title for this post. Finally I use "Auto injecting dependencies in PHP objects". I know it isn't very descriptive. Let me explain it a little bit. This time I want to automate the Hollywood Principle ("Don't call us, we'll call you"). [...] We need to use Reflection to create our instance and to call our action. Sometimes I need to work with custom frameworks and legacy PHP applications. I've done it in a couple of projects, but now I want to create a library to automate this operation.

He includes a simple example of a "Controller" class that is injected with a "Request" via constructor injection. He refactors this to create the dependency injection container with a "Builder" class) and shows how to fetch the instance of the "Bar" class from it. It's this Builder class he shares on GitHub.

0 comments voice your opinion now!
dependency injection container pimple builder class tutorial

Link: http://gonzalo123.com/2014/03/03/auto-injecting-dependencies-in-php-objects/

Benjamin Eberlei:
Feature Flags and Doctrine Entities
December 06, 2013 @ 09:40:00

In a new post to his site Benjamin Eberlei takes a look at the idea of "feature flags" (settings to turn on and off major features) and how they can be used with Doctrine entities to handle sync issues between new properties and the database schema.

The problem of feature flags with Doctrine is easily explained: If you add properties for a new feature that is disabled in the Doctrine metadata, then you need to upgrade the database before deployment, even when the feature is not being rolled out for some days/weeks. Doctrine requires the database to look exactly like the metadata specifies it.

His solution was to use the "loadClassMetadata" event in the entity to dynamically append these new properties based on simple "if" checks of feature flags in the configuration object. Obviously using this is a bit of a hack until the new properties are in place, but once they are then the only change is removing this code.

0 comments voice your opinion now!
feature flag doctrine entities class metadata if check

Link: http://www.whitewashing.de/2013/12/05/feature_flags_and_doctrine_entities.html

Christian Weiske:
PHP Cannot access property started with '\o'
November 08, 2013 @ 09:59:13

Christian Weiske had an interesting situation pop up in one of his applications around a call to a variable with an interesting name.

Some days ago I saw the following fatal error for the first time in my life:

Fatal error: Cannot access property started with 'o' in file.php

After some debugging, I found out that the source of the error was not some strange BOM or UTF-8 characters in PHP source code files. No, it was a combination of protected class properties, object-to-array casting and automatic template property mappings.

As it turns out, there was a change in how object-to-array casting was done in PHP 5.3 that made this break (related to things appended to private and protected variable names). He includes a bit of sample code to illustrate the problem - a simple class converted from object to array with direct casting. He does point out that it doesn't happen with get_object_vars, though, as that doesn't do the casting, just extraction.

0 comments voice your opinion now!
class property special character private protected casting

Link: http://cweiske.de/tagebuch/php-property-started-nul.htm

PHPixie:
Benchmarking Autoloading vs Combining classes into a single file
October 10, 2013 @ 11:48:44

On the PHPixie site there's a new post comparing the overall performance of autoloading versus a single-file approach when it comes to getting the best for your application. They point to the Fat-free Framework as an example of the single file approach.

Thinking about ways of further improving PHPixie I started looking at other projects for inspiration. For example the Fat-Free framework boasts on being contained in a single file. This got me thinking about making a tool for merging all project classes together with vendor libraries into a single for performance boost. MAking such a tool is a fairly trivial task, but still I wanted to be sure it would actually be useful, so I decided to benchmark autoloading classes with composer vs combining them into a single file.

The test script for the benchmarking is included in the post as well as the results from their test runs. In some the XCache extension was used to cache the opcodes, showing a noticeable change from the single-file approach. There's also a measurement of an average framework (autoloaded) request versus the all-in-one method with slightly surprising results. The post also ends with a recommendation for the Composer users out there - using the optional flag to generate a classmap to (slightly) help with autoloading speed.

0 comments voice your opinion now!
benchmark autoload phpixie combined class performance

Link: http://phpixie.com/blog/benchmarking-autoloading-vs-combining-classes-into-a-single-file/


Community Events





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


language bugfix voicesoftheelephpant release list tips laravel api opinion community interview series podcast deployment symfony library package install framework introduction

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