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

Exakat Blog:
5 usages of static keyword in PHP
Jun 20, 2018 @ 15:28:21

On the Exakat blog there's a new post sharing five uses of the "static" keyword in PHP applications. This includes the less common static closures and static class names.

Static is a PHP keyword with many usages. It is almost universally used, though there are many variations of it. Let’s review all the five of them :
  • static method
  • static property
  • static closure
  • static variable
  • static as a classname

The post goes through each of the items in the list giving a brief explanation of how it's used and a code example showing it in action. The post finishes with some tips on evaluating your own code for the use of "static" and tips for each to make your code easier to wrangle and maintain in the future.

tagged: static keyword example tutorial method property closure variable classname

Link: https://www.exakat.io/5-usages-of-static-keyword-in-php/

Exakat Blog:
6 good practices for "use" in PHP
Oct 14, 2016 @ 15:49:51

On the Exakat blog there's a new post sharing six good practices for "use" in PHP. The "use" keyword has a few different places it is used in PHP (like importing namespaced classes and passing in values to closures).

While reviewing code recently, I realized there are no guidelines for use. Every modern code do use ‘use’ (sic), as importing classes from composer or a framework is the norm. There are now quite a few variations in syntax for importing classes or traits. Although the impact on performance is low, as use is solved out, having a clean approach to ‘use’ is important to keep the code clean and readable. Let’s review what are the six good usage of use.

Each of the six tips they share come with a bit of explanation and code to back them up:

  • Do not import unused classes
  • Alway use alias
  • Place all use at first
  • Avoid using the same alias for different classes
  • Group use expression for easy reading
  • Limit the number of use expression

Some of them could be argued as to whether or not they're a "best practice" but it'd definitely interesting to see some tips for the use of this increasingly common little keyword.

tagged: bestpractice use statement keyword top6 import alias opinion

Link: https://www.exakat.io/6-good-practices-for-use/

PHP.net:
PHP 5.5.0 Alpha4 released
Jan 24, 2013 @ 15:29:39

On PHP.net today they've announced the tagging and release of the latest alpha for the PHP 5.5.0 series - PHP 5.5.0 alpha4:

The PHP development team announces the release of PHP 5.5.0alpha4. This release fixe some bugs from alpha3 and add some new features. All users of PHP are encouraged to test this version carefully, and report any bugs in the bug tracking system.

There's new improvements included in this non-production release including class name resolution with "class" keyword and the DateTimeImmutable class. You can see the NEWS file for the complete list of changes. If you'd like to help test it out, you can download the preview release here (or here for Windows users).

tagged: alpha release language class keyword datetimeimmutable test

Link:

Sherif Ramadan:
Finally Getting finally In PHP?
Aug 09, 2012 @ 15:53:55

In this recent post to his site Sherif Ramadan looks at a proposal that's currently under view (RFC) to add the "finally" keyword to PHP.

It’s quite possible that PHP may finally be getting the addition of the finally block in its try/catch block. [...] It also solves a simple, but overlooked problem for the developer. With finally we offer the user-space code a chance to do any clean up work that may be necessary after a try block has terminated execution and with clear semantics.

He includes a use case for this feature - an example showing exception handling on multiple levels and writing to log files when the cleanup of the exception is finished (without the potential for another method to trigger the exception itself). "Finally" allows you to take this logic out of the exception handling and put it at the end, removing the possibility of it triggering an exception for the wrong reason. There's a few other examples showing some other quirks with its usage - like calling die will not make it end up in the "finally".

tagged: finally rfc keyword exception handling proposal

Link:

Sameer Borate's Blog:
Grabbing the referrer search engine keywords for a site
Oct 18, 2011 @ 18:25:27

On his blog today Sameer Borate has a new post with a handy bit of code you can use to find the keywords from a search engine referral to help with tracking how visitors have come to your site.

A couple of weeks back I had to write a solution for a client to track the referrer search engine from where the user came to his sites contact page, without using Google Analytics. If a user was to fill the contact form on the website, the referring search engine name and the keyword for which it was refereed was to be emailed along with the contact information. The following is a solution for the same.

The code itself is pretty simple - it checks the $_SERVER['HTTP_REFERER'] and, based on an array of search engine types, looks for a certain "query" keyname in the URL and matches what follows (with a regular expression). This can be useful for not only determining what sort of audience is visiting your site, but could also be used to present a custom message to visitors from certain search engines (or, more complicated, to show different content based on search terms).

tagged: search engine keyword referrer url snippet

Link:

Evert Pot's Blog:
Taking advantage of PHP namespaces with older code
Feb 01, 2011 @ 16:10:35

Evert Pot has a quick post about a suggestion mentioned at PHPBenelux related to using namespaces with older code.

If you're running PHP 5.3 and you have to use pesky old code that uses long class prefixes (yea, so, pretty much all PHP code out there), you can still make use of namespace features to shorten them.

He includes a quick example that shows the shift from using the traditional Zend_Controller_Action_Helper_AutoComplete_Abstract to an aliasing with the use/as to just reference it as AutoComplete.

tagged: namespace old code zendframework use keyword

Link:

SitePoint PHP Blog:
How to Use PHP Namespaces (Parts 2 &3)
Jul 15, 2009 @ 13:16:24

The SitePoint PHP blog has posted the next two parts of their series looking at using namespaces in PHP - parts 2 and 3:

In the second part of the series they build on the basics and look at importing namespaces into a script, aliasing them to a shorter, easier to use name and some rules to consider about name resolution.

The third part of the series (the last part) looks at the keywords the namespace functionality uses and how to autoload namespaced classes to keep their namespacing intact.

tagged: autoload keyword alias import tutorial namespace

Link:

NETTUTS.com:
The Best Ways to Fight Spam
Oct 06, 2008 @ 14:30:28

Spam is a constant burden for anyone on the web, be it through email or via something like comments on a website. The NETTUTS site can't help so much with the first one, but they've come up with a new tutorial that can help with the second with a few methods.

Deciding on the best method of spam prevention on your blogs, forums, or even contact forms can be difficult. In this article we will take a look at a service called Akismet and how it can help. We will also look at why some other methods of fighting spam fail.

They give a few ways to waylay the spammers including:

  • Disallowing multiple consecutive submissions
  • Keyword Blacklist
  • CAPTCHA
  • and a service called Akismet

They go into a bit more detail on this last one, even going so far as to included code (this class) and examples of how to let the Akismet service see if something is spam or not. Links to libraries for other languages are included too.

tagged: fight spam captcha consecutive keyword blacklist akismet tutorial

Link:

Matthew Weier O'Phinney's Blog:
Migrating OOP Libraries and Frameworks to PHP 5.3
Jul 02, 2008 @ 15:24:02

Matthew Weier O'Phinney recently posted about a method he's come up with for migrating your object-oriented libraries (including frameworks) over to the upcoming PHP 5.3 version of the language.

With PHP 5.3 coming up on the horizon, I'm of course looking forward to using namespaces.

He gives an example of how useful these namespaces can be for you and your code, but points out one failing point - trying to define classes in a namespace that are named the same as a built-in keyword for PHP. There's already been a suggestion to add a captial "I" in front of the class name to prevent this collision.

There's also the problem of throwing custom exceptions - unless you use the namespace properly your script will just throw a default exception.

tagged: php5 namespace migrate library framework collision keyword exception

Link:

IBM developerWorks:
Use the YouTube API with PHP
Apr 17, 2008 @ 03:42:36

In this new tutorial on the IBM developerWorks website, they show you how - with a little simple HTML and PHP - to integrate functionality from the YouTube API into your site.

The YouTube video sharing site allows Web application developers to access public content through its REST-based developer API. [...] This article introduces the YouTube Data API, demonstrates how you can use it to browse user-generated video content; access video metadata, comments and responses; and perform keyword searches.

They help you get started by outlining the format that the YouTube messages use (Atom feeds) and how to run a query against the API and return back the custom data for things like video categories, popularity and the results of keyword searches.

tagged: youtube api tutorial category keyword search atom feed

Link:


Trending Topics: