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

Freek Lijten:
Sane defaults over Exceptions
Jan 18, 2017 @ 10:19:13

In a new post to his site Free Litjen talks about defensive programming and the part that sane default handling plays when dealing with exceptions that might pop up.

With over half a million visitors a week and lots of scrapers, bots and other stuff visiting, these exceptions and fatal errors clog up logging quite a bit. Not to the point that we can't handle the volume, but it generates false positives in monitoring channels and it is something we do not want to act upon anyway.

So while I'm happy to see some defensive programming I would be even happier if exceptional situations would be silently resolved to default situations.

The post starts with a quote about defensive programming and how, despite it not being an ideal use, many applications had been seen using exceptions to handle errors and messaging. He proposes another methodology where a set of default values are used instead of just failing on any error hit with the input. The idea has merit but it can also lead to other frustrations like hidden errors in testing and situations where an exception makes more sense than a default.

tagged: sane default value exception error handling defensive programming

Link: http://www.freeklijten.nl/2017/01/04/Sane-defaults-over-Exceptions

TutsPlus.com:
Programming With Yii2: Using the Debugger
Jan 03, 2017 @ 11:52:29

The TutsPlus.com site continues their "Programming with Yii2" series of tutorials with the latest edition in the series, a look at using the Yii2 debugger to help track down issues in your code.

In today's tutorial, I'll show you how to install the Yii2 Debugger, and we'll explore its capabilities in detail. It's a powerful programming assistant which is often underutilized.

As you code and test your application, the debugger can show you your application path, queries and query variables, performance characteristics, server and framework settings, and much more.

The article starts off by showing you how to install the "yiisoft/yii2-debug" library (via Composer) and enable it in the "debug" section of your configuration. With it correctly installed you'll then be able to access it at the bottom of your pages. The debug console provides plenty of helpful information related to:

  • the route matched
  • a log of actions and events
  • load time
  • memory used
  • database queries performed

...among others. The tutorial walks you through these and other pieces of information the tool provides with screenshots of example output for each.

tagged: programming yii2 debugger tutorial series toolbar screenshot detail

Link: https://code.tutsplus.com/tutorials/programming-with-yii2-using-the-debugger--cms-26910

Medium.com:
The Art of Defensive Programming
Dec 30, 2016 @ 12:59:38

In this post on Medium.com author Diego Mariani talks about the "Art of Defensive Programming" as it relates to the security of the code developers write.

Why don’t developers write secure code ? We’re not talking yet another time about “clean code” here. We’re talking about something more, on a pure practical perspective, software’s safety and security. Yes, because an insecure software is pretty much useless.

[...] Why do I think Defensive Programming is a good approach to issue these problems in certain kind of projects? [...] I personally believe this approach [of continued functionality even in unforeseen circumstances] to be suitable when you’re dealing with a big, long-lived project where many people are involved. Also for instance, with an open source project that requires a lot of extensive maintenance.

He then covers some of what he sees as key tenets of programming defensively:

  • Never trust user input
  • Use database abstraction
  • Don’t reinvent the wheel
  • Don’t trust developers
  • Write SOLID code
  • Write tests

For each item in the list he provides a brief summary of the idea behind it and, in some places, some example code to help illustrate the point. The examples are in PHP but the principles could be applied to just about any language.

tagged: defensive programming tutorial security tenets

Link: https://medium.com/web-engineering-vox/the-art-of-defensive-programming-6789a9743ed4#.u3bzu5xam

TutsPlus.com:
Programming With Yii2: Helpers
Dec 27, 2016 @ 11:13:44

The TutsPlus.com site has continued their "Programming with Yii2" tutorial series with a new post introducing you to helper functions that come bundled with the framework. It also shows you how to create your own custom helper function, making it easier to perform common functions custom to your application.

In the Programming With Yii2 series, I'm guiding readers in use of the Yii2 Framework for PHP. In this tutorial, I'll provide a brief overview of helpers. In Yii, helpers are modules which group commonly useful libraries for string, file, image, URL and HTML management amongst other things, and they are easy to extend.

I'll also share an example of creating a helper within Meeting Planner, the focus of our Envato Tuts+ startup series.

The tutorial then introduces you to (and links you to) the common helpers the framework comes bundled with including the ArrayHelper, FileHelper and Markdown helpers. The tutorial then covers two in a bit more detail, the ArrayHelper and HtmlPurifier helper, including some code examples for each. Finally it shows the creation of a custom helper with several custom functions and their use in a template.

tagged: programming series yii2 framework helpers custom tutorial

Link: https://code.tutsplus.com/tutorials/programming-with-yii2-helpers--cms-26889

TutsPlus.com:
Programming With Yii2: Routing and URL Creation
Dec 13, 2016 @ 12:15:02

The TutsPlus.com site has posted the next article in their "Programming with Yii2" series, this time focusing on the routing and URL creation in the application and how the requests get to the intended functionality (in controllers).

In today's tutorial, I'll review routing and URL creation in Yii. When a browser request arrives at your Yii application's index.php file, it must be parsed to determine which controller and method to call. That's routing. The reverse process of linking to parts of your application is URL creation, which is best done programmatically.

Yii provides a lot of flexibility in managing routing and generating links. Follow me as I review the basics.

He starts with a bit of background on the URL manager that comes built in to the Yii2 framework and how the routes are defined. He then starts in by defining some of the desired routes for the application and how to set them up in the main configuration file (including defaults). The tutorial also includes configuration examples of "pretty URLs", and outputting custom URLs using the "Url" helper.

tagged: programming yii2 series routing url creation tutorial

Link: https://code.tutsplus.com/tutorials/programming-with-yii2-routing-and-url-creation--cms-26869

TutsPlus.com:
Object-Oriented Autoloading in WordPress, Part 3
Dec 01, 2016 @ 11:15:35

TutsPlus.com has continued their series covering object-oriented development practices in WordPress (plugins) with this third tutorial. In previous parts they set up the environment and introduced some of the basic concepts of OOP programming and getting the first classes and files defined.

In the last tutorial, we reviewed the original state of our autoloader and then went through a process of object-oriented analysis and design. The purpose of doing this is so that we can tie together everything that we've covered in this series and the introductory series.

Secondly, the purpose of doing this in its own tutorial is so we can spend the rest of this time walking through our class, seeing how each part fits together, implementing it in our plugin, and then seeing how applying object-oriented programming and the single responsibility principle can lead to a more focused, maintainable solution.

They start with a brief review of what they've covered so far and begin to build on the changes suggested in the previous part of the series. They've already broken it down into the different functional classes (according to the single-responsibility principle) and take the next step of including them and calling some example code to prove all is working as expected.

tagged: oop wordpress tutorial series objectoriented programming plugin part3

Link: https://code.tutsplus.com/tutorials/object-oriented-autoloading-in-wordpress-part-3--cms-27515

TutsPlus.com:
Programming With Yii2: Automated Testing With Codeception
Nov 17, 2016 @ 10:37:40

TutsPlus.com has continued their series looking at programming with the Yii2 framework with this new article covering testing the application with Codeception.

In this Programming With Yii2 series, I'm guiding readers in use of the Yii2 Framework for PHP. In this tutorial, I'll explore automated testing using Codeception, which is integrated with` the Yii2 development framework.

Admittedly, my experience writing tests with my code is scarce. I've often been part of solo or small projects with limited resources. During my time at Microsoft, we had distinct test teams that did this. But frankly, this is likely typical of you too, right? Programmers like to code, they don't write tests—at least old school programmers didn't.

He briefly talks about some of his own experiences with Codeception and how it "makes writing tests fun and easy". The tutorial then moves over to the technical side, showing how to install the Codeception tool and set up the Yii2 application to integrate with its testing functionality. The remainder of the tutorial is split up into the different kinds of testing (unit, functional and acceptance) and some code examples of each.

tagged: yii2 framework series testing codeception programming tutorial

Link: https://code.tutsplus.com/tutorials/programming-with-yii2-automated-testing-with-codeception--cms-26790

TutsPlus.com:
Programming With Yii2: Using Ajax
Nov 10, 2016 @ 11:18:23

The TutsPlus.com site continues their series covering development with the Yii2 framework in this new article. This time they're focusing in on only the Ajax functionality using it an an example for their startup application interacting with Google Maps to place meeting locations.

In this Programming With Yii2 series, I'm guiding readers in use of the Yii2 Framework for PHP. In this tutorial, we'll explore the implementation of interactive pages using Ajax. Specifically, I'm going to highlight the use of Ajax in two areas of the Meeting Planner application, which I'm writing the Building Your Startup series about in parallel.

The article then starts in on showing you how to load a Google Map into the page using some of the built-in Ajax handling and integrating it into the "Create a Place" form. They show how to collect the information from the map once a location is selected and use this to update a "meeting" record on the backend. All coded needed to reproduce the system is included.

tagged: ajax yii2 framework programming tutorial series google map

Link: https://code.tutsplus.com/tutorials/programming-with-yii2-using-ajax--cms-26663

TutsPlus.com:
Programming With Yii2: Security
Nov 09, 2016 @ 12:41:30

The TutsPlus.com site has posted the next article in their "How to Program with Yii2" series of tutorials, this time talking about security covering security tools and functionality already included in the framework.

In this Programming With Yii2 series, I'm guiding readers in use of the Yii2 Framework for PHP. If you're planning to share your application with the public, you'll need it to be secure, and it's best to plan this from the beginning. Fortunately, starting with a framework such as Yii makes this a lot easier than it otherwise would be.

[...] In this tutorial, I'll walk you through the basic security concepts within the Yii application framework. And, if you're interested, future episodes will work to <a href="http://code.tutsplus.com/tutorials/building-your-startup-security-basics--cms-26702>secure the application, Meeting Planner, featured in our startup series, as it approaches alpha release.

The tutorial starts with a look at some of the basics of Yii2's security functionality including authorization tools, password handling and cryptography. Code is included in each section showing the use of the component/functionality. The final point, "Best Practices", links to pages in the Yii2 documentation where you can get more information about preventing vulnerabilities like SQL injection, cross-site scripting and file exposure issues.

tagged: programming yii2 tutorial series framework security controls

Link: https://code.tutsplus.com/tutorials/programming-with-yii2-security--cms-26701

Jason McCreary:
Practicing YAGNI
Aug 10, 2016 @ 10:18:33

Jason McCreary has written up a post covering a popular topic from the eXtreme programming world, a talk he presented on the subject and a bit of his own personal experiences with it: YAGNI or "You Aren’t Gonna Need It".

Last week I spoke at Laracon US 2016 about Practicing YAGNI. First, let me say it was an honor to present for such a large audience at such a premiere conference. I received a lot of feedback and interest in my talk. To that point, many people have asked me to share my slides. As the slides were mostly placeholders for discussion, I felt a blog post would better summarize the talk.

[...] YAGNI is a principle of eXtreme Programming - something I practice daily at work. YAGNI is an acronym for You Aren’t Gonna Need It. It states a programmer should not add functionality until deemed necessary. In theory, this seems straightforward, but few programmers practice it.

He talks about practicing YAGNI and why it's hard for the average developer. He starts with the overall problem it solves and the more relatable KISS (Keep it simple, stupid) and MVP (minimum viable product) realms of thought. He then gets into some of the ways that you can practice YAGNI in your own development, mostly dealing with the timing of feature development rather than complexity. He also includes some times when it doesn't make sense to practice YAGNI and, finally, what practicing it means to him personally.

tagged: yagni yaaintgonnaneedit development principle extreme programming opinion

Link: http://jason.pureconcepts.net/2016/08/practicing-yagni/