Blog Posts
Globals, continued
Update: Sara has pointed out a flaw in my last case. The file
loadFileWithGlobals.php
was incorrectly loading the wrong file — it should
be loading withGlobals2.php
(updated now). When it does, access to baz2
works as it should.
As I note to in my comment, however, I stand by my original rant: relying on globals for your applications is a bad practice, as it makes them difficult to integrate with other applications later. Developers using your application should not need to hunt down exactly when a global is first declared and explicitly push it into the global scope in order to get that application to integrate with others. Use other means, such as singletons or registries, to persist configuration within your applications.
In my last entry, I evidently greatly simplified the issue to the point that my example actually didn't display the behaviour I had observed. I'm going to show a more detailed example that shows exactly the behaviour that was causing issues for me.
First off, this has specifically to do with including files from within functions or class methods that then call on other files that define values in the global scope. In the original example, I show an action controller method that includes the serendipity bootstrap file, which in turn loads a configuration file that sets a multi-dimensional array variable in the global scope. Without first defining the variable in the global scope, this method of running serendipity fails.
Now, for the examples.
PHP globals for the OOP developer
Update: I evidently simplified the issue too much, and have had several
people rightly comment on the bogosity of the issue. However, there are still
situations where $GLOBALS
does not act as expected, and I outline these in
my next entry.
In my previous entry, I ranted about the use of globals in popular PHP applications, and how they make embedding said applications difficult. I develop using object-oriented practices, and can honestly say I can't recall ever having slung a global variable around in my own code. Globals seem hackish to me, and as a result, trying to get applications that use them to behave correctly has been a challenge.
One of the applications I had in mind was Serendipity, the software that powers this blog. I was attempting to create a Zend Framework action controller that wraps my s9y instance so that I can do things such as apply ACLs from my website to selected entries, as well as pull the sitewide skeleton out from s9y so that I only have to maintain one version of it (I had one version for s9y, and another for my own content featured on the site (resume, contact form, etc.).
I tried importing the various config files into my action method prior to
invoking the actual s9y bootstrap, but no dice. I also tried modifying the s9y
config files to use the notation $GLOBALS['serendipity']
around the
serendipity configuration variables (s9y uses a single multi-dimensional array
for all configuration options). This didn't work, either; s9y functions that
called global $serendipity
were still getting a null value.
So, I did a little closer reading in the manual section on predefined
variables, I discovered
something interesting in the description of $GLOBALS
(emphasis mine):
Contains a reference to every variable which is currently available within the global scope of the script.
Interestingly, the section on variable scope didn't make this distinction at
all. Basically, if the variable you reference via $GLOBALS
does not already
exist, assigning it does nothing. It doesn't even raise a notice. It just
silently goes ahead, leaving you thinking you set a new global variable, but in
fact, you cannot assign new globals via $GLOBALS
; you can only modify
existing variables in the global scope.
So, I got around the issue by putting this in my front controller bootstrap:
$serendipity = null;
After that, I was able to create a wrapper action controller for s9y very easily:
/** Zend_Controller_Action */
require_once 'Zend/Controller/Action.php';
/**
* Serendipity integration
*
* @uses Zend_Controller_Action
*/
class S9y_IndexController extends Zend_Controller_Action
{
public function init()
{
// New ViewRenderer helper in ZF incubator; telling it not
// to autorender a view script when done
$this->_helper->viewRenderer->initView(null, null, array('noRender' => true));
}
public function indexAction()
{
global $serendipity;
chdir($_SERVER['DOCUMENT_ROOT'] . '/path/to/s9y');
include './index.php';
chdir($_SERVER['DOCUMENT_ROOT']);
}
}
Note that I don't do any output buffering; this is because the ZF dispatcher takes care of that for me. All I need to do is execute the s9y bootstrap.
So, the lesson to learn from all this: if you need to wrap an application that uses globals, find out what all of them are, and declare them in the global namespace — just setting them to null is enough — in your application bootstrap.
Start Writing Embeddable Applications
Clay Loveless wrote last year an article entitled Stop Writing Loner Applications in which he ranted about all the monolithic applications that act like they're the only kid on the block when it comes to user authentication. Basically, if you want to create a site that utilizes several third-party, off-the-shelf PHP apps (say, a forum, a blog, and a wiki), getting a shared authentication to work between them can be more than a little painful.
I've hit a similar problem repeatedly the past couple months: most of these apps simply are not embeddable, at least not without modifying the source.
"Why embed?" you ask. Simple: if I'm creating a site that has one or two of these applications, but also my (or my company's) own custom functionality, I may want to ensure that certain elements are present on all pages, or that I can control some of the content in all pages: a unified header and footer, ability to inject statistic tracking javascript, etc.
The predominant attitudes are either, "Don't embed our app, embed your app in ours," or "Just modify the templates." Neither of these solutions is acceptable.
BostonPHP Framework Presentations
Last Wednesday evening, I had the honor of presenting Zend Framework to BostonPHP, as part of an ongoing series they're holding on PHP frameworks; Horde was also represented as part of the evening's fare. It was the first time I've attended a UG, so I got the double whammy of that and being a presenter. Oh, make it a triple whammy — Boston is a 3+ hour drive from the Burlington, VT area I now call home.
All in all, the presentations went quite well. It was particularly fun to see what my friend Chuck Hagenbuch has been up to with Horde; his description and demonstration of RDO, or "Rampage Data Objects" was very cool (I really like the idea of "Horde on the Rampage" -- it's a very funny image for me), as was his working demonstration of using WebDAV to pull results via PHP in the Mac Finder.
A lot of people are interested in and working regularly with Zend Framework, at
least based on the questions I was receiving. Attendees ranged from the "what
does Zend do" category to the "We're standardizing on Zend Framework and use
Zend products throughout our stack" category. The bulk of the comments I
received were either of the flavor "I really like what I'm seeing" or wondering
how mature/stable Zend_Db
is. Unfortunately, at the time I was preparing the
slides, there were many features in Zend_Db
that can cause headaches, and I
took some time to point these out; however most of these are soon to be
history, due to the work of Bill Karwin and Simon Mundy, who are pushing to get
a stable, usable DB abstraction layer out the door for ZF 1.0.
During the joint question and answer session, I started getting some particularly tough, pointed questions from one member of the group. I wasn't getting particularly rattled, but the moderator, Seth, decided to intervene and introduce me to my interlocutor — none other than Nate Abele of the CakePHP project. In the end, he joined Chuck and myself at the front of the room, and we had a nice panel discussing how the various frameworks handle different issues.
If you're ever in the Boston area, check to see if BostonPHP is having a meeting; it's a fun group.
My slides are now available; I've revised them slightly to fix some syntactical errors I noticed during the presentation, but otherwise they're what I presented. You may also want to check out the podcast.
Back on Linux Again
A little over a year ago, I stopped using Linux as my primary desktop due to the fact that a number of programs we were using were Windows dependent. Despite getting coLinux running, I've never been completely satisfied with the setup. I missed being able to paste with my middle-mouse button, and I was constantly having character encoding issues pasting back and forth between PuTTY and windows apps, couldn't access mail easily between my coLinux and Windows partitions, and overall felt that I was losing out on some productivity by not having a native linux environment as my primary OS.
Last week, we had an infrastructure change at work, and I basically realized that my Windows + coLinux setup was going to get in the way of productivity -- and that, at this point, there were now Windows applications tying me to that OS. So, I decided it was time to go back to Linux.
Extending Singletons
This morning, I was wondering about how to extend a singleton class such that
you could retrieve the new class when retrieving the singleton later. In
particular, Zend_Controller_Front
is a singleton, but what if I want to
extend it later? A number of plugins in the Zend Framework, particularly view
helpers and routing functionality, make use of the singleton; would I need to
alter all of these later so I could make use of the new subclass?
For instance, try the following code:
class My_Controller_Front extends Zend_Controller_Front
{}
$front = My_Controller_Front::getInstance();
You'll get an instance of Zend_Controller_Front
. But if you do the following:
class My_Controller_Front extends Zend_Controller_Front
{
protected static $_instance;
public static function getInstance()
{
if (null === self::$_instance) {
self::$_instance = new self();
}
return self::$_instance;
}
}
$front = My_Controller_Front::getInstance();
You'll now get an instance of My_Controller_Front
. However, since
$_instance
is private in Zend_Controller_Front
, calling
Zend_Controller_Front::getInstance()
will still return a
Zend_Controller_Front
instance — not good.
However, if I redefine Zend_Controller_Front::$_instance
as protected, and
have the following:
class My_Controller_Front extends Zend_Controller_Front
{
public static function getInstance()
{
if (null === self::$_instance) {
self::$_instance = new self();
}
return self::$_instance;
}
}
$front = My_Controller_Front::getInstance();
Then the any time I call getInstance()
on either My_Controller_Front
or
Zend_Controller_Front
, I get a My_Controller_Front
instance!
So, the takeaway is: if you think a singleton object could ever benefit from extension, define the static property holding the instance as protected, and then, in any extending class, override the method retrieving the instance.
exuberant ctags with PHP in Vim
One reason I've heard PHP developers use for adopting an IDE when developing is the ability to click on a class or function name and jump to the declaration. Sounds like magic, and it's definitely something I've desired.
One way I get around it is by adopting PEAR coding standards for naming my
classes. Since they define a one-to-one mapping of class name to the file
system (substitute the underscore character (_
) with the directory
separator), I can usually very quickly and easily open a class file,
particularly if I start in the base directory of the project install.
Today, however, I found exuberant ctags, a library which can be used to generate an index file mapping language objects to source files and the line in the source file where they are declared. Contrary to its name, it's not just for the C language; it currently supports 33 different programming languages, including PHP.
I decided to try it out on the Zend Framework core library today. At first run, it was pretty useful. However, it was only mapping classes, and, in addition, only those defined with the single word 'class' — abstract classes and interfaces were entirely left out. So, I looked into the documentation to see if I could change the behaviour.
And, being a Unix program, of course I could. First off, you can add functions to the items it indexes with a simple flag. Additionally, you can use POSIX regular expressions to refine what it searches.
I whipped up the following script to create my tags index:
###!/bin/bash
cd /path/to/framework/library
exec ctags-exuberant -f ~/.vim/mytags/framework \
-h \".php\" -R \
--exclude=\"\.svn\" \
--totals=yes \
--tag-relative=yes \
--PHP-kinds=+cf \
--regex-PHP='/abstract class ([^ ]*)//c/' \
--regex-PHP='/interface ([^ ]*)//c/' \
--regex-PHP='/(public |static |abstract |protected |private )+function ([^ (]*)//f/'
This script creates the tag index in the file $HOME/.vim/mytags/framework
. It
scans for PHP files recursively through the tree, excluding any files found in
a .svn
directory (I'm using a checkout from the subversion repository). The
file paths in the index are created relative to the tags file; this was
important, because if this wasn't provided, vim was unable to jump to the file,
as it couldn't find it. --PHP-kinds=+cf
tells it to index classes and
functions. Next, I've got three regular expressions. The first tells it to
match classes beginning with 'abstract class' as classes. The second tells it
to match interfaces as classes. The last is so that PHP 5 methods, which begin
with a visibility operator, to be matched as functions.
Once the index file is generated (it takes less than a second), all you need to
do in vim is tell it to load it: :set tags=~/.vim/mytags/framework
. At this
point, you can do all sorts of fun stuff. Place the cursor on a class name or
method name, anywhere in it, and hit Ctrl-]
, and you'll jump to the file and
line of its declaration; Ctrl-T
then takes you back. If you change the
invocation to Ctrl-W ]
, it will split the current window and open the
declaration in the new pane. (If you're familiar with how help works with Vim,
this should seem pretty familiar.)
One more reason to stick with Vim for your PHP editing needs. :-)
PHP decoding of Javascript encodeURIComponent values
Recently, I was having some issues with a site that was attempting to use UTF-8 in order to support multiple languages. Basically, you could enter UTF-8 characters — for instance, characters with umlauts — but they weren't going through to the web services or database correctly. After more debugging, I discovered that when I turned off javascript on the site, and used the degradable interface to submit the form via plain old HTTP, everything worked fine — which meant the issue was with how we were sending the data via XHR.
We were using Prototype, and in particular, POSTing
data back to our site — which meant that the UI designer was using
Form.serialize()
to encode the data for transmission. This in turn uses the
javascript function encodeURIComponent()
to do its dirty work.
I tried a ton of things in PHP to decode this to UTF-8, before stumbling on a solution written in Perl. Basically, the solution uses a regular expression to grab urlencoded hex values out of a string, and then does a double conversion on the value, first to decimal and then to a character. The PHP version looks like this:
$value = preg_replace('/%([0-9a-f]{2})/ie', \"chr(hexdec('\1'))\", $value);
We have a method in our code to detect if the incoming request is via XHR. In
that logic, once XHR is detected, I then pass $_POST
through the following
function:
function utf8Urldecode($value)
{
if (is_array($value)) {
foreach ($key => $val) {
$value[$key] = utf8Urldecode($val);
}
} else {
$value = preg_replace('/%([0-9a-f]{2})/ie', 'chr(hexdec($1))', (string) $value);
}
return $value;
}
This casts all UTF-8 urlencoded values in the $_POST
array back to UTF-8, and
from there we can continue processing as normal.
Man, but I can't wait until PHP 6 comes out and fixes these unicode issues…
Overloading arrays in PHP 5.2.0
Update: I ran into issues with the ArrayObject
solution, as there was a bug in PHP 5.2.0 (now fixed) with its interaction with empty()
and isset()
when used with the ARRAY_AS_PROPS
flag. I tried a number of fixes, but eventually my friend Mike pointed out something I'd missed: as of PHP 5.1, setting undefined public properties no longer raises an E_STRICT
notice. Knowing this, you can now do the following without raising any errors:
class Foo
{
public function __set($key, $value)
{
$this->$key = $value;
}
}
$foo = new Foo();
$foo->bar = array();
$foo->bar[] = 42;
This is a much simpler solution, performs better, and solves all the issues I was presented. Thanks, Mike!
svn:externals
I was recently working with someone who was using Zend Framework in their project. To keep things stable and releasable, he was doing an export of framework into his repository and checking it in. Since files change so much in the ZF project currently, instead of doing an rsync from a checkout into his own repository, he decided instead to delete the directory from the repository and re-add it everytime he was updating framework.
This seemed really inefficient to me, especially considering that it made it incredibly difficult to merge changes from his development branch into his production branch (deleting and re-adding directories breaks the merge process considerably). I knew there had to be a better way.
I'd heard of the svn:externals
property before, but never really played with
it. As it turns out, it exists for just this very type of situation. The
problem is that the documentation of svn:externals
in the SVN book doesn't indicate at all how the property should be set, and
most howto's I've read omit one or more very important details. I finally
figured things out through some trial and error of my own, so I'm going to
share the process so others hopefully can learn from the experience as well.
It's actually pretty easy. This assumes that your project layout looks something like this:
project/
branch/
production/
tag/
trunk/
-
In the top of your project trunk, execute the following:
$ svn propedit svn:externals .
-
This will open an editor session. In the file opened by your editor, each line indicates a different external svn repo to pull. The first segment of the line is the directory where you want the pull to exist. The last segment is the svn repo URL to pull. You can have an optional middle argument indicating the revision to use. Some examples:
-
Pull framework repo from head:
framework http://framework.zend.com/svn/framework/trunk
- Pull framework repo from revision 2616:
framework -r2616 http://framework.zend.com/svn/framework/trunk
-
-
After saving and exiting, update the repo:
$ svn up
-
Commit changes:
$ svn commit
One thing to note: any directory you specify for an svn:externals
checkout
should not already exist in your repository. If it does, you will get an
error like the following:
svn: Working copy 'sharedproject' locked
svn: run 'svn cleanup' to remove locks
I show using revisions above; you could also pin to tags by simply checkout the external repository from a given tag. Either way works well.
Then, when moving from one branch to another, or from the trunk to a branch,
you simply set a different svn:externals
for each branch. For instance, your
current production might check from one particular revision, but your trunk
might simply track head; you then simply determine what the current revision
being used is on your trunk, and update svn:externals in your production branch
when you're ready to push changes in.
Hope this helps some of you out there!