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

Lorna Mitchell:
Using Composer in an Existing Project
Aug 20, 2013 @ 15:18:38

Lorna Mitchell has a recent post to her site showing you how to use Composer within an existing project that might not have the same Composer-expected structure. Composer is a package and dependency manager for PHP.

I've got an application (okay, scratty PHP script) which glues together some API things and shows them onto a dashboard for me. Recently, I updated it to use Guzzle as the consuming client, since twitter now needs me to authenticate (I wrote about that if you're interested), and I used Composer to bring the new library in. It was very simple so I thought I'd share it as it's quite minimal example, and those are my favourite kind.

She includes a brief "getting started" for those not already familiar with Composer and shows a sample composer.json file that pulls in the Guzzle HTTP library. Then calling the "install" with Composer pulls in the files in the right place and all you have to do is add the needed require_once include to your autoloading process. Composer handles the rest.

tagged: composer existing project guzzle install introduction requireonce

Link: http://www.lornajane.net/posts/2013/using-composer-in-an-existing-project

Anthony Ferrara:
Is Autoloading A Good Solution?
Jul 20, 2012 @ 20:56:30

In his most recent post Anthony Ferrara takes a look at autoloading - mostly asking the question of whether the problems it has really outweigh the benefits.

The real problem that autoloaders solve is to load dependencies. [...] The normal logic that's used to justify autoloading over manual dependency loading in production is that it only loads the classes you need. Therefore you save the parsing costs of classes that you don't need. But surely that additional run-time loading has costs. So I decided to setup a test to see how expensive that additional run-time loading costs us, and to prove whether or not autoloading is worth it in production.

He gives an example of the two methods - using the spl_autoload_register method to define a loader and loading them with a defined file path instead. He found the autoloading version slower than the hard-coded (by quite a bit) but how, when the number of files is reduced, the performance gets much closer. He also briefly looks at two other pieces of file-related functionality: file_exists and require_once.

tagged: autoload solution performance static path fileexists requireonce

Link:

Robert Basic's Blog:
Using the new autoloaders from Zend Framework 1.12
Jun 22, 2012 @ 13:17:05

Robert Basic has a new post today about the autoloaders in Zend Framework 1.12 and how to use them to create a classmap for use in your application.

The latest, and last, release of the Zend Framework 1.x series is just around the corner as ZF 1.12.0RC1 was announced this week. As I still have projects running ZF1 I thought about giving the most interesting new feature (for me) a spin - the new autoloaders which are backported from ZF2. I decided using the classmap autoloader as the main autoloader, and the good ol' standard autoloader as the fallback autoloader.

He includes the changes to the Front Controller (index.php) to have it know about these new autoloaders and has a command that will go through your code any pull out any require_once statements out and let the autoloader handle it instead.

tagged: zendframework autoloader classmap tutorial requireonce

Link:

Propel Blog:
The End of Autoloading
Mar 25, 2011 @ 16:13:51

On the Propel blog there's a recent post talking about how the age of autoloading might be ending and how namespacing could be the next logical step (or could it).

Autoloading in PHP is a great time saver. It lets you write concise scripts without the knowledge of the exact directory structure of the libraries you use. But with the arrival of namespaces in PHP 5.3, and the influence of Java over new generation PHP frameworks, autoloading is changing. In the near future, explicit autoloading will be ubiquitous, but with none of the advantages of the old style autoloading.

He talks about "the old days" when things were included manually through file paths, how that graduated to the SPL autoloading and, most recently, up to namespace autoloading. He shares code samples of how the namespace loading works and how you can abuse it to override current classes/functionality with your own. He points out one interesting correlation though - that the "use" keyword seems a lot like the "require_once" of way back when. He shows how the added verbosity of namespace usage can be a hinderance on frameworks, citing microframeworks specifically and showing one implementation that's non-namespaced next to another that is.

tagged: autoloading namespace requireonce use spl

Link:

Smashing Magazine:
Ask SM [PHP]: Form Validation, Converting MySQL to XML
Feb 06, 2009 @ 19:48:11

Smashing Magazine has started off a new feature of their site - an "Ask SM" where they gather questions their readers ask about different topics like CSS, Javascript and, of course, PHP. This new post is one such response. There's five quick questions answered in this new post:

  • Form validation with PHP
  • Converting MySQL to XML
  • require_once()-problem
  • Search in different tables?
  • Getting information out of an XML-file
Here at Smashing Magazine, we want to help out PHP programmers who are just getting started or who want to improve their programming chops. Our goal is to support our community by answering their questions and trying to find solutions to their problems.

You can submit your questions in one of two ways - either via their forum or by sending a tweet (that's on Twitter to him @jasonatennui.

tagged: form validation convert mysql xml requireonce search database

Link:

Joakim Nygard's Blog:
Optimizing PHP Through Habits
Apr 25, 2007 @ 15:39:00

Spurred on by some previous benchmarks [pdf] from Ilia Alshanetsky, Joakim Nygard decided to run some his own benchmarks on the same sort of functionality.

There are numerous discussions in the blogosphere about whether to use echo versus print, if for() is faster than while(), etc. and though the gains are usually very small, I decided to add my thoughts to the debate. I found an article on optimization through coding habits in Ilia Alshanetsky's zend performance slides.

According to his results:

  • Calling require_once() 10000 times in a for() loop with an empty file is 4x faster.
  • With a simply autoload requiring a class and 10000 loops of new Foo() versus require_once('foo.php'); new Foo() shows that __autoload() is ~3.7 times faster.
  • If a class method can be static, declare it static. Speed improvement is by a factor of 4.
  • Avoid function calls within for() loop control blocks
  • Always, always quote array keys.
  • Get rid of 'harmless' error messages - they take time to generate and output.

I am not out to prove Ilia wrong - he knows PHP better than most - and for all I know, they could have optimized those very functions in PHP 5.2. [...] It would appear that there are improvements, albeit small, to achieve from minimal effort. Plus I was surprised by the discrepancies I found compared to Ilia's recommendations.
tagged: optimize coding habit benchmark requireonce autoload loop optimize coding habit benchmark requireonce autoload loop

Link:

Joakim Nygard's Blog:
Optimizing PHP Through Habits
Apr 25, 2007 @ 15:39:00

Spurred on by some previous benchmarks [pdf] from Ilia Alshanetsky, Joakim Nygard decided to run some his own benchmarks on the same sort of functionality.

There are numerous discussions in the blogosphere about whether to use echo versus print, if for() is faster than while(), etc. and though the gains are usually very small, I decided to add my thoughts to the debate. I found an article on optimization through coding habits in Ilia Alshanetsky's zend performance slides.

According to his results:

  • Calling require_once() 10000 times in a for() loop with an empty file is 4x faster.
  • With a simply autoload requiring a class and 10000 loops of new Foo() versus require_once('foo.php'); new Foo() shows that __autoload() is ~3.7 times faster.
  • If a class method can be static, declare it static. Speed improvement is by a factor of 4.
  • Avoid function calls within for() loop control blocks
  • Always, always quote array keys.
  • Get rid of 'harmless' error messages - they take time to generate and output.

I am not out to prove Ilia wrong - he knows PHP better than most - and for all I know, they could have optimized those very functions in PHP 5.2. [...] It would appear that there are improvements, albeit small, to achieve from minimal effort. Plus I was surprised by the discrepancies I found compared to Ilia's recommendations.
tagged: optimize coding habit benchmark requireonce autoload loop optimize coding habit benchmark requireonce autoload loop

Link:


Trending Topics: