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

Simon Holywell:
PHP and immutability
Mar 22, 2017 @ 16:21:37

In a recent post to his site Simon Holywell covers immutability in PHP. PHP, by default, uses weak typing and doesn't support much in the way of immutability but Simon shows you a few ways you can get around this and make immutable objects you can use and extend.

Being a weakly typed dynamic language, PHP has not really had the concept of immutability built into it. We’ve seen the venerable define() and CONSTANTS of course, but they’re limited. Whilst PHP does ship with an immutable class as part of it’s standard library, DateTimeImmutable, there is no immediately obvious method to create custom immutable objects.

[...] It is possible to write your own immutables using some simple and sneaky PHP techniques though. We’re going to use a simplistic data requirement to make the examples in this article easier to follow. I’ll be using professional skateboarders and the tricks that they brought to the world.

He starts the article talking about immutability and how it relates back to the current (as of PHP 7) values supported in constants - scalars and arrays (no objects). He then starts on the code to create the base Immutable class that sets its values via the constructor. He then points out some of the common "work arounds" people use when trying to work with immutable objects and some techniques to help prevent it: the use of final, a "flag" preventing another constructor call, etc.

tagged: immutable tutorial technique php7 constant

Link: https://www.simonholywell.com/post/2017/03/php-and-immutability/

QaFoo Blog:
Basic Refactoring Techniques: Extract Method
Mar 09, 2017 @ 17:24:35

On the QaFoo blog there's a tutorial posted to their blog sharing one of their basic refactoring techniques: the "extract" method:

Refactoring is the process of restructuring code without changing its behaviour and the technique "Extract Method" is one of the most important building blocks of refactoring.

With extract method you move a fragment of code from an existing method into a new method with a name that explains what it is doing. Therefore this technique can be used to reduce complexity and improve readability of code.

In this post I want to explain the mechanics of extract method using an example so that you have a checklist of steps when performing this refactoring. Extract method is a technique that you can use even without tests, because the potential risks of breaking are manageable when you follow the steps.

While things like PHPStorm provide their own "extract" functionality to help with refactoring, they want you to understand the manual steps involved. They start with a sample method that mixes controller logic with low level query logic. They break the process down into a few steps:

  • Identify code fragment to extract
  • Create empty method and copy code
  • Identify undeclared variables that must be arguments
  • Identify variables that are still used in old method
  • Call new method from original method

They also include a "risky" checklist with a few questions to ask about the refactor to see what extra steps might need to be considered.

tagged: refactor extract method technique tutorial

Link: https://qafoo.com/blog/098_extract_method.html

NetTuts.com:
Advanced CodeIgniter Techniques and Tricks
Jun 07, 2012 @ 18:04:01

On the NetTuts.com blog today there's a new handy post for the CodeIgniter users out there - some more advanced tips and tricks that you can use in your CI-based apps.

CodeIgniter is a widely used PHP framework that aims to help developers write better structured code and remove boilerplate tasks from the workflow. In this article, I’m going to explain some incredibly useful techniques to use when developing applications using CodeIgniter.

They cover things like setting up easier configuration parameters, optional config settings, creating HTML emails, application versions and separate response formats.

tagged: codeigniter technique trick advanced tutorial

Link:

BinaryTides.com:
40+ Techniques to enhance your php code (3 Part Series)
Apr 11, 2012 @ 14:52:57

On the BinaryTides blog there's a series of posts that share some tips and suggestions aimed at helping you and your code be the best they can be - things to enhance your application (including suggestions not just about code but also about environment and development practices).

The three posts in the series include tips like:

  • Maintain debugging environment in your application
  • Collect all output at one place , and output at one shot to the browser
  • Set the correct character encoding for a mysql connection
  • Do not gzip output in your application , make apache do that
  • Don't check submit button value to check form submission
  • Process arrays quickly with array_map
  • Avoid direct SQL query , abstract it
  • Never set error_reporting to 0
  • Make a portable function for executing shell commands

Obviously, not all of these will apply in all situations, but they're an interesting list. Most will come with good explanations and code samples when appropriate.

tagged: technique enhance list suggestion bestpractice

Link:

Pim Elshoff's Blog:
Dependency management
Feb 14, 2012 @ 18:04:18

Pim Elshoff has a recent post to his blog about dependency injection in PHP applications and some of the good and bad things about implementing it. He includes examples of a few different types of "injection" ranging from using globals to an actual dependency injection container.

This article describes common pains and symptoms of bad dependency management and common techniques for dealing with project-wide dependencies. [...] For the purpose of this article I am talking about classes using other classes. To manage your softwares dependencies then becomes designing your software architecture such that the depencies are good. The following sections will talk about what's bad, with good being the absense of bad.

In his "problems" section he includes things like "it's easy to do wrong", "it's hard to get rid of", the "low rate of reuse in bad DI" and how bad DI can make for "untestable software". He points to a few packages (including PHP_Depend) that can be used to determine your dependency levels. His methods for "injection" include using the global scope (so bad), using injection in a constructor/setter, making a service locator (registry) and using a full-blown dependency injection container.

All techniques, tricks and gimmicks aside, managing dependencies is more about actual design than abstract design patterns. You yourself are responsible for keeping the number of dependencies as low as possible and designing your system such that access to dependencies is provided in a sane, understandable manner.
tagged: dependency management injection bad technique architecture

Link:

Gonzalo Ayuso's Blog:
Populating datagrid techniques with PHP
Jul 19, 2011 @ 14:25:46

In a new post to his blog Gonzalo Ayuso looks at the code required to populate a jQuery data grid with the records as pulled from a (MySQL) database.

Today I want to speak about populating datagrid techniques with PHP. At least in my daily work datagrids and tabular data are very common, because of that I want to show two different techniques when populating datagrids with data from our database. Maybe it's obvious, but I want to show the differences.

He uses "old school spaghetti code" rather than a framework to keep things simple and pulls the data from the database with a PDO connection. This information is then manually pushed into an HTML table and the data grid functionality is applied to it. The other method involves a little bit of JSON magic that the data grid library pulls in and populates for you, still appending rows to a table.

He notes that the second method seems faster to the user since the page and table are rendered first, but it also comes at the cost of more than one HTTP request.

tagged: datagrid jquery technique preload ajax json

Link:

Eran Galperin's Blog:
Database Optimization Techniques You Can Actually Use
Mar 28, 2011 @ 18:15:57

Eran Galperin has posted a few helpful tips (ones you can "actually use") for optimizing your database and how your application uses it.

I understand the need for simplicity because of the wide audience of Smashing Magazine, but I'd wish they'd give something more than the absolute basics you could find in almost any other site out there [in their recent tutorial]. I also didn't like some of the methods mentioned there for profiling (or the code itself), so I here is my starter guide to optimizing database performance.

Tips included in his post center around a different set of questions:

  • When should you optimize the database?
  • Think about profiling first, optimizing last
  • How EXPLAIN can find the big problems
  • When caching should be used

Some code is included, but only briefly since his point is more about the server than the client (script) side of things.

tagged: database optimize technique explain cache profile

Link:

Rafael Dohms' Blog:
Problem Solving technique #1: Taking a mental break
Feb 07, 2011 @ 18:08:44

Rafael Dohms has posted a new tip for developers (and really anyone else that has to concentrate on mentally challenging tasks all day) that could sometime help you break through that wall you've hit in your code - take a mental break.

Developers are modern day artists whose masterpieces are not hung on walls but stretched out thin on web servers all over the world, yes that is very poetic, but I really try to look at developers as artists and puzzle solvers. [...] A different activity, to most its the simple act of going to get coffee or water, taking a stroll outside in the fresh air, some like sports, some like games.. everyone has their escape valve.

He gives an example of a friend (Chester) who, when he hit a mark he just couldn't pass, turned to a set of lego blocks to help clear his mind. The subconscious mind kicks in as you preoccupy the conscious and a lot of times the answer floats up to he top with little or no effort.

tagged: problem solving technique mental break developer

Link:

Simas Toleikis' Blog:
PHP data caching techniques
Dec 13, 2010 @ 14:45:10

Simas Toleikis has posted some caching techniques he's come up with to handle a few different situations including simple file-level caching and working with memcached.

Caching intermediate data is the most primitive and yet the most rewarding optimization technique where you don’t need to fiddle with any complex algorithms or premature optimizations. If you are into PHP you should be aware of all the best practices and the right tools for this job to make your websites fly.

He covers a few different, though common, situations you may come across in your application where caching could be very helpful:

  • Static scope variables
  • APC shared memory functions
  • Memcached for large distributed caches
  • In-memory database tables
  • Simple file-level caches
tagged: caching technique situation apc memcache file memory

Link:

PHPClasses.org:
Lately in PHP Ep. 6 - Unusual Site Speedup Techniques, Named params & Annotations
Nov 01, 2010 @ 13:29:46

On the PHPClasses.org site today they've posted the latest episode in their "Lately in PHP" podcast series - "Unusual Site Speedup Techniques debate, Named parameters and Annotations".

In this episode, Manuel Lemos and Ernani Joppert discuss several unusual site speedup techniques presented in recent articles of the PHPClasses site blog. They also discuss the (non-)inclusion of new PHP features discussed by PHP core developers like having named parameters in function calls and the support of Java-like annotations in PHP code.

To listen you can either use the in-page player, grab it from iTunes or just download the mp3. Complete show notes and transcript are also included in the post.

tagged: podcast latelyinphp speedup technique named parameter annotation

Link:


Trending Topics: