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

Programming With Yii2: Building Community With Voting, Comments, and Sharing
May 19, 2017 @ 10:27:21

The TutsPlus.com site has posted the latest in their "Programming with Yii2" series of tutorials with this new article looking at features for the application useful to build community around the site.

In today's tutorial, I'm going to show you how to extend Yii to easily mimic a site like Reddit with voting, comments, and sharing.

[...] I built some my latest Twitter API episodes on the early version of this platform, following friends on behalf of users and analyzing our followers. The site I described in those, Twixxr, forms the foundation of my Yii customization work.

So adding core functionality like voting, comments and sharing makes so much sense. As you expand your Yii codebase with these kinds of features, building new sites becomes faster, easier and increasingly powerful.

He then spends the rest of the article showing how to use three Yii2 plugins to help provide the community functionality he needed: a voting extension, a plugin for integrating Disquis and a "social extension" for other social plugins and widgets. He shows how to get these extensions installed and what code updates you'll need to make to use them across the application. This also includes screenshots of the results so you can be sure that you're on the right track.

tagged: tutorial series yii2 community voting comment disquis sharing

Link: https://code.tutsplus.com/tutorials/programming-with-yii-building-community-with-voting-comments-and-sharing--cms-27798

Ignace Nyamagana Butera:
Regular expressions documented in PHP
Oct 20, 2015 @ 10:35:09

In this post to his site Ignace Nyamagana Butera shares a a helpful thing you can do with the regular expression handling in PHP to help make it a bit more clear: embedded comments.

One of the most challenging aspect of using regular expressions is documenting them. More than often you end up with a complex expression that even you as a creator have a hard time documenting. While reading sitepoint recent article around regular expressions I was intrigued that this article did not feature any tips on how to document them. So let’s take a very complex regular expression and see how we could improve its documentation.

He gives an example of a complex regular expression used to parse a URI into its different parts, including an example URI and the resulting parsed array. He points out that, while the regular expression works fine, it's easy to forget what each part does and as it is quite complex. To help remedy this, he includes an example of a commented regular expression (available since PHP 5.2) where you have two options:

  • add in named subpatterns with a special < and > notation
  • put in literal comments by breaking up the regex into multiple lines and using the hash (#) to mark of the comment at the end of the line

He includes code examples of both of these, resulting in a much clearer, memorable regular expression where the increased number of lines is a good trade-off for clarity.

tagged: regularexpression regex document comment named subpattern tutorial

Link: http://nyamsprod.com/blog/2015/regular-expressions-documented-in-php/

SitePoint PHP Blog:
Commenting, Upvoting and Uploading Photos with the 500px API
Mar 10, 2015 @ 11:55:46

The SitePoint PHP blog continues their series creating a simple application based on the 500px service with part two, commenting, upvoting and uploading photos. The 500px service is a photo community for discovering, sharing, buying and selling inspiring photography powered by creative people worldwide.

In the first part of this series we used the API to get the latest photos from 500px and filter through them, and we built a user profile with the list of associated photos. In this part, we are going to give the user the ability to vote, favorite and comment on photos, and finally, we will give the users the ability to upload their own photos.

They start with a look at authentication and authorization handling to make the OAuth-based request to the 500px API. Code is included for the connecting class, including the addition of a logger to make debugging the requests simpler. From there they set up the routes for handling votes on photos (PHP and Javascript) and marking favorite photos. They also include the functionality for adding comments, getting the latest comments and uploading new photos to the service. The final resulting code can be found over on GitHub.

tagged: 500px tutorial api series part2 comment upvote upload photos

Link: http://www.sitepoint.com/commenting-upvoting-uploading-photos-500px-api/

Anthony Ferrara:
A Point On MVC And Architecture
Dec 02, 2014 @ 12:10:24

Anthony Ferrara has posted another in his series looking at MVC as a design pattern and as an idea for building web applications. In this latest post he goes on to make a point about MVC, how it relates to architecture and CRUD.

Last week I post a post called Alternatives To MVC. In it, I described some alternatives to MVC and why they all suck as application architectures (or more specifically, are not application architectures). I left a pretty big teaser at the end towards a next post. Well, I'm still working on it. It's a lot bigger job than I realized. But I did want to make a comment on a comment that was left on the last post.

He responds to the comment (essentially that CRUD is a solved problem) and where the need for customizations is needed. He suggests what the real problem is, though: the three classes of developers - CMS users, custom developers and users of both.

tagged: mvc architecture opinion problem crud comment response

Link: http://blog.ircmaxell.com/2014/12/a-point-on-mvc-and-architecture.html

Paul Jones:
A Response To "On php-fig and Shared Interfaces"
Dec 24, 2012 @ 12:54:57

Paul Jones has written up a response to Matthew Weier O'Phinney's recent post on shared interfaces and the PHP-FIG. In it he talks about the PHP-FIG group itself and specific references back to the original post.

He mentions the ideas of "new thinking" and the limitations that standardized interfaces might try to impose on an application:

One is able to imagine reasons why having shared interfaces of the kind described above is in opposition to, or at best orthogonal to, better development practices and greater innovation across PHP land. Even so, I assert that shared interfaces as described, while maybe preventing an imaginable ideal in theory, instead promote an actual good in practice.

Matthew Weier O'Phinney responded with some of his own comments and correcting some of the misinterpretation of his original comments.

tagged: phpfig shared interface response comment


Anthony Ferrara's Blog:
To Comment Or Not To Comment - A Tale Of Two Stories
Jun 08, 2012 @ 11:02:20

In this recent post to his blog Anthony Ferrara looks at commenting in your application's code and how to be effective (and not so effective) when using them.

A few weeks ago I was sparked into a twitter conversation with Larry Garfield (@Crell) about the value of comments in code. [...] A pretty innocuous comment [about code that doesn't have comments] that is quite insightful. [...] That led to an interesting discussion that just couldn't fit on twitter. So let me explain...

He defines what he means by "comments" (DocBlock, general comments and legal information) and what sort of things should be inside of them. He also points out a few things not to do in you comments including being overly descriptive and restating what the code is doing (they should be less about that and more about the "Why"). Of course, there's exceptions and he touches on those briefly to finish out the post.

tagged: comment code opinion useful


Gonzalo Ayuso's Blog:
Inject dependencies via PhpDoc
Apr 10, 2012 @ 10:23:14

Gonzalo Ayuso has a new post to his blog looking at a method for injecting dependencies into your application's code based on comments in the PHPDocumentor-formatted comments of your methods.

Last month I attended to Codemotion conference. I was listening to a talk about Java and I saw the “@inject” decorator. I must admit I switched off my mind from the conference and I started to take notes in my notebook. The idea is to implement something similar in PHP. It’s a pity we don’t have real decorators in PHP. I really miss them. We need to use PhpDoc. It’s not the same than real decorators in other programming languages. That’s my prototype. Let’s go.

All of the code you'll need to recreate his solution is included - a sample "User" class that needs a valid PDO object in a private "db" property, a "DocInject" class that parses the comments and, using a new feature of PHP 5.4 (traits), injects the needed functionality into the "User" class and creates/assigns the object.

You can see just the full code in these two gists on Github.

tagged: phpdocumentor comment tutorial injection traits


Rafael Dohms' Blog:
Book Review: The Art of Readable Code
Feb 29, 2012 @ 10:41:12

Rafael Dohms has posted a new review of a book that focuses on helping you create better, more readable code - "The Art of Readable Code" (Dustin Boswell, Trevor Foucher, O'Reilly). This is isn't about "pretty code" as much as it is manageable, easy to follow structures and logic flows.

"The Art of Readable Code" was written by Dustin Bowell and Trevor Foucher and basically focuses on concepts and suggestions to make you code not just readable, but comprehendible by other developers, or as the author’s suggest, yourself in six months. Code readability is a topic that I truly believe the PHP community does not focus enough on and i really wanted a look at this book to see what kind of ideas it had and what I could do my best to bring to the attention of other developers.

The book is language-agnostic and provides ideas that developers should keep in mind when doing their development - clear variable names, making comments that make sense, refactoring tips and hints for implementing your ideas in code. He recommends the book to any developer (in any language) to help them make code that will stand the test of time and be easier to manage/understand in the future.

tagged: code readable book review oreilly clean comment naming refactor testing


Building a Domain Model - An Introduction to Persistence Agnosticism
Feb 27, 2012 @ 12:58:00

On PHPMaster.com there's a recent tutorial introducing the concept of a "domain model" and showing how to create them in PHP (manually, not inside of any ORM or database solution).

First off, creating a rich Domain Model, where multiple domain objects with well-defined constraints and rules interact, can be a daunting task. Second, not only is it necessary to define from top to bottom the model itself, but it’s also necessary to implement from scratch or reuse a mapping layer in order to move data back and forward between the persistence layer and the model in question.

They include an example of a set of domain models tat relate to one another - a blog setup with posts, comments and users. They show how to create the AbstractEntity to handle a bit of the magic behind the scenes, an example "Post" and "Comment" models and how they can be put to work creating some posts and appending comments. A little bit of markup is included to output the results.

tagged: domain model relation tutorial blog post comment user


Introduction to PhpDoc
Jan 10, 2012 @ 10:07:26

On PHPMaster.com today there's a new post from Moshe Teutsch about working with docblock comments in PHP scripts and how to use the phpDocumentor tool to generate the documentation from them.

If you’ve ever tried to read code written by someone other than yourself (who hasn’t?), you know it can be a daunting task. [...] PhpDoc, short for PhpDocumentor, is a powerful tool that allows you to easily document your code via specially formatted comments. [...] By using PhpDoc, you can make it easy for others (and yourself) to understand your code - weeks, months, and even years after you’ve written it.

He introduces the concept of "docblocks" and includes several examples of how to comment things like packages, files, classes and functions/methods. Finally, he wraps up the post with an example of using the "phpdoc" command to run phpDocumentor and build the docs. In the comments, another tool is also suggested - DocBlox, a project that still parses the same docbloc syntax but does it in a much more memory efficient way (and is an actively maintained project).

tagged: docbloc introduction phpdocumentor docblox comment phpdoc