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:
When to Use Static Methods
June 16, 2014 @ 10:20:52

Mathias Verraes has followed up his previous post about named constructors in PHP with a bit more clarification about when to use static methods (as he did in his "multiple constructor" examples previously).

Some of the reactions to my last blog post on Named Constructors in PHP, originate from the notion that static methods are inherently bad and should never be used. This is rather overgeneralized. Static methods are nothing more than namespaced global functions. Namespacing, I think we can all agree on, is great. As for global functions: We use those all the time. The native functions in PHP form our basic building blocks.

He talks about the main problem with their use, the shared global state, and compares it to a more stateful service. His solution is to either move to a normal object state (that allows for internal tracking) or think more about abstractions and how they relate.

0 comments voice your opinion now!
static methods opinion object stateless abstraction

Link: http://verraes.net/2014/06/when-to-use-static-methods-in-php/

Mathias Verraes:
Named Constructors in PHP
June 13, 2014 @ 09:42:15

Mathias Verras has a new post to his site about an idea he calls "named constructors". This method uses static factory methods to simulate the idea of a constructor and initialize the object.

PHP allows only a single constructor per class. That's rather annoying. We'll probably never have proper constructor overloading in PHP, but we can at least enjoy some of the benefits. Let's take a simple Time value object. Which is the best way of instantiating it? The only correct answer is "it depends".

His example shows the typical constructor creation with variable arguments, but points out that this can get messy quickly. His other method, the factory methods as "constructors", can make for a cleaner interface and makes the class more flexible. They make the object able to be initialized with different types of values and even satisfies the Single Responsibility Principle. He goes through a few examples using his "Time" class, showing how different "constructor" methods can be used to handle inputs ranging from a normal hour/minute format out to a "from minutes since midnight" value.

0 comments voice your opinion now!
named constructor factory method static tutorial time

Link: http://verraes.net/2014/06/named-constructors-in-php/

Codacy.com:
Review of PHP Static Analysis Tools
May 09, 2014 @ 11:35:15

The Codacy.com blog has posted a review of various static analysis tools for PHP-based applications. These tools can help provided quality and consistency in your code in a more automated way.

Maintaining code quality over time is a hard challenge. It becomes even harder in large projects developed by many programmers. Each person has different code styles and different ways to approach problems. Over time, this may result in confusing and unmaintainable code. Static analysis tools can help developers solve this problem, they enforce coding standards, detect common errors and cleanup code blocks.

Tools mentioned in the post include: PHP_CodeSniffer, the PHP Mess Detector and the PHP Copy & Paste Detector. Each comes with an example of the command to execute it and some sample results. They also talk briefly about where and how these tools could fit into your current workflow, either during development or as a part of a full deployment process.

0 comments voice your opinion now!
static analysis tool list review standards quality integration

Link: http://blog.codacy.com/2014/05/06/php-static-analysis-tools/

Software Testing Magazine:
PHPUnit Tips & Tricks
October 11, 2013 @ 09:42:44

The Software Testing Magazine site has posted some helpful tips for using PHPUnit in a new post. This is part of a series - the first post introducing PHPUnit and the second showing how to use mocks and stubs.

I have showed how to set up PHPUnit and how to get started with unit tests and how to handle mocking and stubbing objects in order to effectively isolate your code under test. This article will explore a few ways to get the most out of your PHPUnit tests.

They cover a few different topics including:

  • Working with static methods
  • Working with frameworks
  • Working with databases
  • Effective testing for current and new projects
0 comments voice your opinion now!
phpunit tips tricks advanced static framework database

Link: http://www.softwaretestingmagazine.com/knowledge/phpunit-tips-tricks/

Russell Walker:
Handling Global Data in PHP Web Applications
September 16, 2013 @ 12:31:07

Russell Walker has a post on his site sharing some suggestions about effectively dealing with global data in your PHP applications.

Almost every web application needs to handle global data. There are certain things that just have to be available throughout the entire code base, such as database connections, configuration settings, and error handling routines. As a PHP developer, you may have heard the mantra 'globals are evil', but this naturally begs the question 'what should I use instead of global variables?'

He includes four different options (five including the actual use of global variables):

  • Static classes
  • Singleton
  • Registry
  • Dependency injection

For each of the options he includes summaries of both the advantages and disadvantages as well as some sample code showing their use. Ultimately, he points out that it's up to the developer of the application which option fits best.

0 comments voice your opinion now!
global variable data opinion options registry singleton dependencyinjection static

Link: http://russellscottwalker.blogspot.co.uk/2013_09_07_archive.html

Phil Sturgeon:
PHP Static Analysis in Sublime Text
August 21, 2013 @ 09:49:59

Phil Sturgeon has put together a new post for his site showing you how to set up static analysis of PHP code in the Sublime Text editor using various tools.

Coding Standards have been around for the longest time and recently they're starting to become more widespread in PHP. While learning Python I really enjoyed how Sublime Text 2 would shout at me for using too many empty lines, using tabs instead of spaces and even things like declaring unused local variables, importing modules that were never used, etc. This was pretty cool, and I was soon writing beautiful Python code without any concern over which way things should be done. I've been doing this with PHP for the last year, but trying to get a new-hire going with this stuff was hard. We smashed through it taking notes so now I've written it up for you guys.

He's broken it down into a few different steps (three of them) to get things like the right version of PHP, PHPMD and PHP_CodeSniffer installed and configured. There's a few config settings you'll need to change in Sublime to get things working correctly, but it's a relatively painless setup.

0 comments voice your opinion now!
static code analysis phpmd phpcs sublimetext install configure

Link: http://philsturgeon.co.uk/blog/2013/08/php-static-analysis-in-sublime-text

Christopher Martinez:
Static code analysis tools for PHP
May 08, 2013 @ 12:38:22

Christopher Martinez has a recent post to his site that covers some of the static analysis tools available for PHP including the PHP Mess Detector, PHP CodeSniffer and the PHP Analyzer.

I believe in writing code that is easy to understand, easy to test, and easy to refactor. Yes, I realize that the statement above is pretty general and open to interpretation. Not everyone needs external tools to ensure quality in their code...but, I work on things from time to time that have absolutely no tests. [...] For whatever reason, this happens a lot more frequently in the PHP world. I'm guilty of not writing tests and checking how I write code, sometimes, too. Things are bright, though, for the PHP community - for quite some time now, we've had fantastic tools that assist us in writing better code.

He covers each of the tools, talks some about what they're good for and gives examples of their use, including output. He also talks some about the Pfff set of tools created by Facebook. He also talks some about how these tools fit into his daily work as a part of his pre-commit hooks in git.

0 comments voice your opinion now!
static code analysis tools description example

Link: http://chrsm.org/2013/05/05/code-analysis-tools-for-php

Benjamin Eberlei:
Traits are Static Access
April 12, 2013 @ 11:16:35

In a new post to his site Benjamin Eberlei shares an opinion about traits, noting that they're basically the same as static functionality when it comes to several things like coupling, not being testable and being a "global state" container.

I used to look forward to traits as a feature in PHP 5.4, but after discussions with Kore I came to the conclusion that traits are nothing else than static access in disguise. They actually lead to the exact same code smells. Familiar with the outcome of too much static use, we should reject traits as just another way of statically coupling your code to other classes.

He includes some code examples showing traits in use in an example controller to handle a simple redirect. He points out at least six different issues with just this simple implementation. He rewrites it as "static" code to help prove his point. He comes to the conclusion that, much like static methods, traits should be avoided and instead aggregation should be favored.

0 comments voice your opinion now!
traits static compare avoid example code

Link: http://www.whitewashing.de/2013/04/12/traits_are_static_access.html

David Zentgraf:
How Not To Kill Your Testability Using Statics
December 05, 2012 @ 11:57:33

If you've been around PHP for any length of time, you know about the static functionality and keyword that the language offers. You might have used it in the past for a few things, but maybe you're not 100% sure of how to use it right. If this describes you, you should check out this article from David Zentgraf for a great summary of their use and how to not kill the testability of your application by using them,

"Class Oriented Programming" is what people do when they write classes which are all static methods and properties and are never once instantiated. I'll try to explain why this adds virtually nothing vis-a-vis procedural programming, what these people are really missing out on by ignoring objects and why, against all odds, statics don't automatically kill testability. While this article focuses on PHP, the concepts apply equally across many languages.

He talks about code coupling, expectations when using static classes/methods and how it seems a little too similar to some of the procedural PHP we all started out writing. He ponts out that most static method calls are more like function calls than true OOP methods and that their dependencies are a bit more difficult to manage. He suggests that statics are really only good for one kind of thing - dealing with already static data and "utility methods" operating on given data.

0 comments voice your opinion now!
static method testability function method oop


Reddit.com:
Avoid static methods at all costs? (testability)
September 26, 2012 @ 11:59:04

On Reddit.com there's a recent post questioning the (recently) common saying that PHP developers should avoid static methods when concerned about testability:

I get it: testing is important, and building your codebase in a manner that is easy to test should be a priority. However, sometimes I feel like I have to compromise on the elegance of my code in order to maintain testability. Cases where perhaps a static method makes sense, but end up having to perform some coding acrobatics in order to avoid it. Is this a common challenge, something many developers face and must balance between? Or am I misguided in how frequently static methods can be the most elegant solution (before taking testability into consideration)?

Answers point out a few things - that sometimes, state doesn't matter and static is okay or that they can be used if the instance they return is always exactly the same, never altered.

0 comments voice your opinion now!
static methods unittest testability opinion



Community Events





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


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

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