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

Ahmad Hajjar:
Mocking Objects for Unit Testing in PHP Using Go! AOP
Aug 18, 2015 @ 11:55:01

In an interesting post to his blog Ahmad Hajjar shows you how to use to Go! PHP AOP framework to help mock objects in your unit tests. The Go! AOP framework provides some of the base functionality for using aspect-oriented programming techniques in your code.

These have been always very important questions in software development industry; what should I test in unit testing? And how can I make tests as small as possible, and how to avoid testing already tested code? or 3rd party libraries? [...] In principle, unit test must fail for one and ONLY ONE reason, this makes sense actually, as it complies with the S in the SOLID object-oriented design principles. [...] We can use mocks in the problem listed above, to make the unit test, real unit test, that is testing only the things that needs to be tested. [...] However, what we are going to do today is something different. OK! Why should we re-invent the wheel? the answer is : because it is simpler wheel ! also it is a wheel that uses different technology which is AOP.

He uses a simple Laravel application and the Go! framework for his testing examples. He uses the AOP functionality from Go! as the mock handling instead of something like PHPUnit's own mocking or Mockery. He shows how to configure the AOP functionality first, then creates a basic aspect class with a few interceptors for method calls. He creates an AopMocker class that adds the class requested to the mock configuration. Finally he improves the initial unit test example using these tools, building mocks for User and Post classes and making the same assertion.

tagged: unittest mock object aop aspectoriented programming goaop framework

Link: https://ashajjar85.wordpress.com/2015/08/16/mocking-objects-for-unit-testing-in-php-using-go-aop/

SitePoint PHP Blog:
The State of Accessibility in PHP Tools
Aug 03, 2015 @ 11:19:21

On the SitePoint PHP blog Parham Doustdar has posted a look at accessibility in PHP tools or how easy they make it for those with disabilities (such as his own blindness) to do their development work.

Usually when I tell people that I’m blind, many people ask me how I can use the computer. “Is someone reading you my messages?” I remember someone asking. Many people imagine that I have this super-nifty speech recognition software that I can just talk to, and it would do anything, even write code. Imagine dictating code to a speech recognition system! [...] I gave an answer on Quora, to someone who had asked How does a visually impaired computer programmer do programming? I recommend you go through that answer to have a better context on what I’ll be talking about in this post.

He starts with a look at how visually impaired people could normally use a computer using screen readers, interaction with the software (all through the keyboard) and some things that just can't be done with this setup. He covers some of the issues screen readers have when parsing web applications and links to the WebAIM articles page for more information there. He then gets into the IDE comparison covering essential, assistance and supplementary features as well as community engagement around accessibility issues. He compares:

  • PHPStorm
  • SublimeText
  • NetBeans
  • Eclipse-based IDEs (Zend Studio, Eclipse PDT)
  • Notepad++

Unfortunately, most of the software on his list received a rating of "zero" on the scale with the exception of Notepad++, though it still has places it falls flat.

tagged: accessibility tools blind programming ide comparison screenreader

Link: http://www.sitepoint.com/the-state-of-accessibility-in-php-tools/

SitePoint PHP Blog:
Defensive Programming in PHP
Jul 21, 2015 @ 11:49:07

In an article from the SitePoint PHP blog author Jeff Smith walks us through some advice he has about defensive programming in PHP, that is good practices for writing code that more gracefully handles potential error points.

Defensive programming, simply put, is programming with the intent to anticipate likely failure points. The goal is to circumvent those likely problems before they occur. You see the problem, right? There’s something inherently difficult with the advice “expect the unexpected” and it’s made many times worse when one alters it to “expect the unexpected and try to prevent it”. Let’s look at some practical examples.

He touches on a few of the most common places where errors could be introduced with unexpected input or functionality:

  • Conditional Statements
  • User Input (and trusting it....hint: never)
  • Assumptions [Made] About Your Code
  • Tunnel Vision (or not using good development practices)
  • Consistency in Syntax and Naming

Each point in the list includes a brief summary of what to look out for and things you can do to prevent the problem. It's by no means an exhaustive list, but it is a good place to start.

tagged: defensive programming tutorial opinion advice

Link: http://www.sitepoint.com/defensive-programming-in-php/

NetTuts.com:
Programming With Yii2: AuthClient Integration With Twitter, Google and Other Networks
Jun 26, 2015 @ 10:58:36

NetTuts.com has posted the latest part of their "Programming with Yii2" series today, this time with a focus on AuthClient integration allowing for easy interfacing with social services like Google or Twitter.

In this Programming With Yii2 series, I'm guiding readers in use of the newly upgraded Yii2 Framework for PHP. In this tutorial, I'll guide you through integration of the built-in AuthClient support to provide sign-in from third party networks such as Twitter, Google and Facebook.

For these examples, we'll continue to imagine we're building a framework for posting simple status updates, e.g. our own mini-Twitter, using our hello codebase. Use the GitHub links on this page to download the code repository. In Programming With Yii2: Integrating User Registration, I walked through integration of the Yii2-User library for user registration and authentication. In this tutorial, we'll learn how to integrate AuthClient with Yii2-User and override its views.

The tutorial starts off with a look at AuthClient: what it is, what it supports out of the box and which they'll be integrating (Google and Twitter). He shows you how to install the library via Composer and where to configure it, including the keys or secrets needed to connect to the provider of choice. For Twitter, he shows how to register a new "application" on their side and how to hook it into your own application and code. He advocates using an ".ini" file outside of the code to store the service credentials too. Finally he shows how to integrate it with the frontend, including the widget to override the default user login handling.

tagged: programming yii2 authclient twitter google social oauth

Link: http://code.tutsplus.com/tutorials/programming-with-yii2-authclient-integration-with-twitter-google-and-other-networks--cms-23489

NetTuts.com:
Programming With Yii2: Rich Text Input With Redactor
Jun 19, 2015 @ 10:38:55

NetTuts.com continues their series about programming with the Yii2 framework in this new article moving on to the use of the Redactor rich text editor that comes bundled with the framework.

In this Programming With Yii2 series, I'm guiding readers in use of the newly upgraded Yii2 Framework for PHP. In this tutorial, I'm going to introduce you to using the rich text editor Redactor within the Yii Framework. For these examples, we'll continue to imagine we're building a framework for posting simple status updates, e.g. our own mini-Twitter.

Thanks to the Yii community purchasing an unlimited license for the Redactor rich text editor, it's easy to install and use via the "yii2-redactor" extension. They help you get it installed and update a sample form to use it instead of the normal basic HTML textarea for content input. They also show you how to add image support and what it looks like both inline in the editor and the resulting HTML output.

tagged: yii2 series tutorial programming redactor richtext editor image

Link: http://code.tutsplus.com/tutorials/programming-with-yii2-rich-text-input-with-redactor--cms-23174

Ed Zynda:
Aspect Oriented PHP And The Interceptor Pattern
Jun 12, 2015 @ 09:51:23

Ed Zynda has a recent post to his site looking at the use of the Interceptor design pattern with the aspect oriented methodology in PHP. He doesn't get into the basic concepts of AOP, so you might want to find out a bit more in other places. It's not a necessity, though, as his code examples are easy enough to follow along.

There are many ways to modify the behavior of existing code with actually changing the core logic. Some patterns you might be familiar with are the decorator pattern or the observer pattern. Both allow you to take another object and modify the behavior by wrapping your modifications around the original code. One pattern you might not be familiar with though, is the interceptor pattern.

He first helps you install the AOP-PHP extension to provide some of the necessary PHP functions. From there he talks about using some of the functions that intercept function calls and perform an action. He shows examples of it using:

  • aop_add_before
  • aop_add_after
  • aop_add_around

He also shows how to modify arguments during execution and changing return variables when the execution of the function is completed.

tagged: aspectoriented programming aop designpattern interceptor

Link: http://www.edzynda.com/aspect-oriented-php-and-the-interceptor-pattern/

NetTuts.com:
Programming With Yii2: Working With Asset Bundles
Jun 10, 2015 @ 10:42:30

NetTuts.com is back with another part in their "Programming with Yii2" series today. In this new post they focus on asset bundles, pieces of functionality that let you easily manage things like Javascript and CSS files automatically.

In this Programming With Yii2 series, I'm guiding readers in use of the newly upgraded Yii2 Framework for PHP. In this tutorial, I'll show you how to add custom JavaScript and CSS scripts and libraries to your Yii application. Yii uses a concept it calls Asset Bundles to make it easier to manage these resources. For these examples, we'll continue to build on the simple status application from previous tutorials.

They start with an introduction to asset bundles and how to set them up in the framework and the resulting HTML markup to include the involved files. They show you how to create a simple bundle and load it into your application. A screenshot is included showing a result of the inclusion.

tagged: yii2 framework series programming asset bundles javascript csss

Link: http://code.tutsplus.com/tutorials/programming-with-yii2-working-with-asset-bundles--cms-23226

NetTuts.com:
Programming With Yii2: Specialized Validations
Jun 03, 2015 @ 10:53:23

NetTuts.com continues their series covering development with the Yii2 framework today with a new post looking at specialized validations (expanding on their previous post covering some of the basic built-in validations).

In this Programming With Yii2 series, I'm guiding readers in use of the newly upgraded Yii2 Framework for PHP. This tutorial is our second part, looking at Yii2's validators. Validators simplify the code needed to validate input, i.e. verify conformance or non-conformance of data input, typically from users via web forms. Specifically, we're going to explore some of the built-in specialty validations that are common to web development.

They cover some of the more complex validators in this tutorial including:

  • CaptchaValidator
  • ExistValidator
  • ImageValidator
  • RegularExpressionValidator
  • UniqueValidator
  • UrlValidator

They cover each of them with a brief summary of what they can do and a code example showing them in action. In some cases (like with the CAPTCHA validator) a screenshot is also included of the output.

tagged: yii2 series tutorial programming advanced validators

Link: http://code.tutsplus.com/tutorials/programming-with-yii2-specialized-validations--cms-23427

NetTuts.com:
Programming With Yii2: Validations
May 27, 2015 @ 11:41:37

NetTuts.com has continued their series around developing applications with the Yii2 framework with this new post covering validations.

In this Programming With Yii2 series, I'm guiding readers in use of the newly upgraded Yii2 Framework for PHP. In this tutorial, I'm going to introduce you to Yii2's validators. Validators simplify the code needed to validate input, i.e. verify conformance or non-conformance of data input, typically from users via web forms. For these examples, we'll continue to leverage the Hello application codebase we've used in past tutorials. Use the GitHub links on this page to get the code.

They start with a brief look at what the validators are and some common uses (and how they can save you time in the long run). There's a list of the current validators included in the framework and how/where they fall in the overall process flow. Then the tutorial gets into the application of a validator, showing how to define the rules, display errors associated with them and application to a "Sample" model and form. This includes the use of the FilterValidator, DefaultValueValidator and SafeValidator.

tagged: yii2 series tutorial programming validators

Link: http://code.tutsplus.com/tutorials/programming-with-yii2-validations--cms-23418

NetTuts.com:
Programming With Yii2: Blameable Behaviors
May 20, 2015 @ 10:33:10

NetTuts.com has continued their series about programming with the Yii2 framework with this new tutorial talking about blamable behaviors. These behaviors can de defined similarly to a common concept in other languages/tools, mixins

In this tutorial, I'll guide you through another of Yii2's interesting behaviors: helping automate the common web development task of assigning created by and updated by user_ids across the models in your web app using DRY coding and Yii2 BlameableBehavior. We'll also create a log that records who updated the Status table for every change made.

In his example he shows the code needed to insert and update these two dates related to users of your system. He shows what kind of updates are needed to support it in your migrations and adding in the support for the BlameableBehavior in the models. He then modifies this example to not only log the latest person to modify a record but a history of IDs in a "status log" table.

tagged: tutorial blameable behaviors programming series yii2 framework

Link: http://code.tutsplus.com/tutorials/programming-with-yii2-blameable-behaviors--cms-23287