News Feed
Jobs Feed
Sections




News Archive
feed this:

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

Rob Allen:
Use statements
March 17, 2014 @ 10:13:08

Rob Allen's latest post focuses in on something that's been a part of PHP for a while now, back when namespacing was introduced - the "use" keyword. He shares some thoughts, both from others and himself, about whether or not they make code more readable.

I was having a discussion on IRC about use statements and whether they improved code readability or not. [...] Those longer class names make it a little hard to quickly parse what it going on. The [example with "use" statements] is clearly less cluttered, but is at the expense of ambiguity. Exactly what class is User? I would have to go to the top of the file to find out. Should I use aliases? If so, how should I name them?

He went out to Twitter for advice from other PHP developers on the issue too. The feedback from his question came mostly in support of the "use" statements:

  • "I think use statements just abstract where the class is coming from. Some people find that useful."
  • "I think it's helpful seeing all of the packages used by a class without having to look through the full code."
  • "One reason I like them is that I can glance at a file and know dependencies immediately."
  • "I do appreciate what you are saying about the indirection use statements introduce."

There's also a bit of talk about "aliasing" with namespaces rather than the full classname, then using the namespace and class name in the code to "minimise ambiguity".

0 comments voice your opinion now!
use statement namespace twitter advice feedback alias

Link: http://akrabat.com/php/use-statements/

Reddit.com:
Attempting to understand Namespacing, and its benefits
January 02, 2014 @ 11:54:23

If you're relatively new to PHP, you may have read about something called "namespacing" but not fully understood it or the benefits it provides. Over on Reddit, there's a recent discussion where the poster asks about just that:

As I understand it, Namespacing provides a lot of useful context for developers, as well as making the autoloading of classes much easier (though I've never personally tried this). I can also see it being used in a large enough application where it might help keep things in scope, but this seems like a bit of a stretch to me. Apart from that, I'm not too clear on what namespacing actually does.

The comments to the thread including things like links to other resources and other suggestions like:

  • tools to try out
  • places to start using it in your own code
  • the difference between underscores and real namespacing
  • how they help avoid conflicts in naming and functionality
0 comments voice your opinion now!
namespace benefit context autoload opinion discussion

Link: http://www.reddit.com/r/PHP/comments/1u1ztr/attempting_to_understand_namespacing_and_its

Paul Jones:
PSR-4 "Autoloader" Has Passed
December 04, 2013 @ 10:37:51

As Paul Jones mentions in his latest post, one of the latest proposals to the PHP-FIG (Framework Interoperability Group) has officially passed, PSR-4, providing a more strict standard for autoloading than the widely used PSR-0.

Counting from the date of that first formal proposal, it has taken exactly 8 months of discussions, one botched vote, one rescinded vote, an entirely new FIG workflow, and four or five rewrites to get PSR-4 passed. Maybe 8 months doesn't sound so long when you look back on it, but while you're in the middle of it, it's interminable.

Paul talks about some of the differences between it and PSR-0, making for "shallower" and more concise directory structures for packages. He also points to some of the packages from the Aura framework as examples of its implementation.

0 comments voice your opinion now!
psr4 autoloader phpfig proposal vote pass directory structure namespace

Link: http://paul-m-jones.com/archives/4804

Community News:
PSR-4 Autloader Proposal now in Draft Status
August 23, 2013 @ 12:05:41

As is mentioned in this Reddit post, the PHP-FIG group has moved a new autoloader structure proposal into "Draft" status, making it past the initial proposal (Entrance Vote) level as PSR-4.

This new autoloader has been in the works for a while, and is meant to be a modern autoloader that reflects the changes in the PHP landscape since PSR-0 was originally put together. Yesterday it passed the Entrance Vote, which means its now a formally worked and supported by the FIG.

While PSR-0 is new to many, its been around for years - since before Composer was really a thing - and PEAR/SF1/ZF1 were the prominent sources of distributed code. Now that so many are building components with Composer and using PSR-0 (yay) it's about time a new standard took care of a few hangovers from the PEAR/Zend style.

The newly proposed autoloader gets rid of some of the allowances that PSR-0 included like proper namespace support (no more underscores) and a reduction in the required folder structure to make it work. The Composer project is also a part of these discussions and are on board with making these proposed changes in the project's autoload handling.

We'd love to get constructive feedback on PSR-4. Post stuff up here, on the mailing list, get in #phpfig on freenode, whatever, just get involved. That doesn't mean everything will be implemented, but there is more chance of the standard containing your feedback if you make it - so post something.
0 comments voice your opinion now!
phpfig psr4 autoloader draft vote composer namespace directory

Link: http://www.reddit.com/r/PHP/comments/1kvr1f/the_new_autoloading_standard_psr4_passes_entrance

The PHP.cc:
PHP 5.5 New CLASS Constant
June 26, 2013 @ 09:02:08

The PHP.cc have posted another article in their series looking at the new features that come with the latest release of PHP (5.5). In this new post they cover the "CLASS" constant.

Last week, the first stable version of PHP 5.5 was released. It introduced a class-level constant, aptly named CLASS, that is automatically available on all classes and holds the fully-qualified name of that class. [...] So why would you need such a constant? [...] When you need the fully qualified name of a namespaced class that is referenced by a namespace alias ... then it gets interesting.

He illustrates with an example of a unit test using stubs and mocks. The normal method requires the definition of the class namespace in the "getMock" call. With the CLASS constant, PHP can extract that information from the namespace referenced in the "use" and drop it in as a replacement.

0 comments voice your opinion now!
class constant php55 new feature namespace unittest

Link: http://thephp.cc/viewpoints/blog/2013/06/php-5-5-new-class-constant

NetTuts.com:
Better Workflow in PHP With Composer, Namespacing, and PHPUnit
January 21, 2013 @ 10:49:15

On NetTuts.com there's a new screencast posted showing you a good way to create a better workflow in your PHP development using Composer and PHPUnit.

In this video tutorial, we'll again focus on workflow. Specifically, we'll use Composer's built-in autoloading capabilities, along with namespacing, to make for an elegant testing experience. We'll also review a handful of PHPUnit best practices, as we get setup with these tools.

He shows you how to use Composer to load in the packages from other projects (as well as your own) and using PHPUnit to execute unit tests for your application. He uses test-driven development, but it's not a required part of the workflow. He helps you create a simple "Calculator" test. He also shows how to manually modify the Composer classmap to load in your own classes.

0 comments voice your opinion now!
workflow screencast phpunit composer namespace tdd unittest


Juan Treminio:
Composer Namespaces in 5 Minutes
October 02, 2012 @ 15:47:32

Juan Treminio has a new post to his site today talking about Composer and namespacing and shows you how the two work together to make using 3rd party tools easy.

You've heard of Composer, right? The nifty new tool for PHP that aims to centralize and streamline package management? Do you also know of, but don't really understand how namespaces work in PHP? Then let's set you straight! In 5 minutes you'll learn how Composer's autoloader and namespaces work!

He's broken it up into a few sections:

  • Getting Composer up and running
  • Making a basic "composer.json" file
  • Going through what the "install" creates (including the "autoload_namespaces.php" file)
  • Using the vendor/ autoloader

As a bonus, he also shows how to implement your own namespacing in the "composer.json" file if you have a special case - just use the "autoload" section in the configuration (examples included).

0 comments voice your opinion now!
composer namespace introduction custom configure


NetTuts.com:
Namespacing in PHP
October 02, 2012 @ 13:48:00

On NetTuts.com today there's a new tutorial introducing you to namespaces in PHP and a complete guide to the features that come with them.

It's been a bumpy ride, in regards to namespace support in PHP. Thankfully, it was added to the language in PHP 5.3, and the applicable structure of PHP code has improved greatly since then. But how exactly do we use them?

They start with a definition of a namespace (for those unfamiliar with the term) and get right into the code showing their use in PHP:

  • Defining a Namespace (and Sub-namespaces)
  • Calling Code from a Namespace (using Unqualified/Qualified/Fully Qualified names)
  • Dynamic calls
  • The namespace Keyword
  • Aliasing or Importing
0 comments voice your opinion now!
namespace tutorial introduction features examples


Michael Nitschinger's Blog:
A primer on PHP exceptions
May 23, 2012 @ 09:17:41

Michael Nitschinger has a new post focusing on one of the more commonly used, but maybe just as commonly misunderstood, part of PHP - exceptions and their handling. His latest post looks at what Exceptions in PHP have to offer and provides some "best practices" in their use.

Exceptions are and should be an integral part of any general purpose programming language. PHP introduced them long ago (with the release of PHP 5 or 5.1), but it still seems that many of the concepts are not fully understood or ignored by the community. This post aims to be a solid introduction to exception architecture, handling and testing. At the end of the post you should be able to know when to raise an exception and how it should look like.

He talks about situations when (and when not) to use exceptions, normalizing them for easier try/catch-ing and includes the exception class hierarchy, including the types pulled from the SPL. He shows examples (based on the Lithium framework's namespacing) how to create "namespaced exceptions" and how to use these in a bit of sample code. He also mentions the use of the custom error handling with the ErrorException as well as a quick look at testing these basic and custom exceptions correctly (PHPUnit-based tests).

0 comments voice your opinion now!
exception custom namespace tutorial primer unittest lithium


Stefan Koopmanschap's Blog:
Using custom namespaces with (C/S)ilex and Composer
April 12, 2012 @ 12:22:47

Stefan Koopmanschap has a quick new post to his blog with a handy tip for Composer and Cilex/Silex users when dealing with custom namespaces.

For a new proof of concept application I'm building, I need both a simple web interface as well as some commandline tools. I decided to use Silex for the web interface and Cilex for the CLI tools, and opted for using Composer for installing these dependencies into my project. I ran into some issues with the custom project libraries I was building for this application however. Registering my custom namespace into Silex and Cilex didn't result in the classes being loaded for some reason. Composer helped me out though.

His solution involves letting Composer be the default autoloader for the application via an "autoloader" configuration option in the "composer.json" (that can also take a classmap option if you're not PSR-0 compliant, see here).

0 comments voice your opinion now!
custom namespace cilex silex composer psr0 autoloader



Community Events











Don't see your event here?
Let us know!


release podcast overview opinion language introduction install composer framework database unittest hack application performance facebook symfony2 component security hhvm package

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework