Zend Framework
An Easier Way to Instantiate Models in Zend Framework Controllers
by Steven Brown on May.24, 2010, under PHP, Zend Framework
I get sick of having to type this:
$someTable = new Some_Table(); $someTable->someMethod();
Since PHP can’t chain on instantiation this gets very annoying.
I added this to my base controller (which all of my controllers extend):
public function __get($name) { if (substr($name, -5) == 'Table') { $tableName = ucwords(substr($name, 0, -5)) . '_Table'; return new $tableName(); } return parent::__get($name); }
Now in my actions I can use a table like this:
$this->someTable->someMethod();
Easy!
If you want to make it more efficient you can store the objects and return them if they are used multiple times:
private $_tables = array(); public function __get($name) { if (substr($name, -5) == 'Table') { if (!isset($this->_tables[$name])) { $tableName = ucwords(substr($name, 0, -5)) . '_Table'; $this->_tables[$name] = new $tableName(); } return $this->_tables[$name]; } return parent::__get($name); }
Installing ZFDebug
by Steven Brown on May.24, 2010, under PHP, Zend Framework
Installing ZFDebug is pretty simple, first of all you’ll want to download it or do a checkout from the ZFDebug Subversion repository.
Personally I added an external to my existing Subversion project, add the following to your svn:externals property in the library folder:
ZFDebug http://zfdebug.googlecode.com/svn/tags/release-1.5/library/ZFDebug
You only want ZFDebug in a development environment, you don’t want it appearing on a live site, so you’ll want to create a configuration option:
zfdebug.active = true
Then you simply register the plugin with your front controller, I do this in my bootstrap:
if (isset($this->_config->zfdebug) && $this->_config->zfdebug->active) { Zend_Controller_Front::getInstance()->registerPlugin(new ZFDebug_Controller_Plugin_Debug(array( 'jquery_path' => 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js', 'plugins' => array( 'Variables', 'Html', 'Database', 'File', 'Memory', 'Time', 'Registry', 'Exception', ), ))); }
Read the ZFDebug documentation for more information on adding filters to the various plugins, and to add custom timers and custom memory usage points to your code.
ZFDebug
by Steven Brown on May.22, 2010, under PHP, Zend Framework
Thanks for Darby Felton for putting me on to this cool Zend Framework debugging utility:
A tutorial should be coming this way once I’ve had time to get it figured out.
Zend Framework Performance Architecture Part 4: Denormalisation using the Observer Pattern
by Steven Brown on Jul.18, 2009, under Zend Framework
In the previous Zend Framework Performance Architecture articles we’ve covered some structural changes you can make to improve the performance of your code by applying caching and denormalisation of data. The examples in these articles made a point, but will quickly become a maintenance nightmare.
One rule I like to follow with my development is to make each class responsible for itself as much as possible, this way I can add/remove/change everything related to that class in one place (mostly). This is generally known as loose coupling.
(continue reading…)
Q&A – Something’s wrong with my output buffering!
by Steven Brown on Jul.14, 2009, under PHP, Zend Framework
Question:
Something’s wrong with my output buffering! I have a Zend Framework project with a long script and I’m trying to output stuff in the controller during the process but it doesn’t appear until the end. I tried using flush() and moving the code to the view but it made no difference. What is going on?
Answer:
First of all Apache on Windows doesn’t respond to flush(), it will always wait until the end of the request to output the result.
For other system though what you should know is that by default the Zend Framework front controller will buffer all output. This is normally fine and actually is quite useful, however there are some situations where you will want to bypass output buffering. In order to do this you should place the following in your bootstrap:
$frontController->setParam('disableOutputBuffering', true);
Now I like to do this only if a certain $_GET variable is sent so that I can apply it to any URL at will, however you might like to use a plugin to permanently disable output buffering for URLs that require it.
Upgrading autoloaders from Zend Framework 1.7 or less to 1.8
by Steven Brown on Jun.15, 2009, under Zend Framework
In versions of Zend Framework prior to 1.8 you could set Zend_Loader as the autoloader using the following:
include 'Zend/Loader.php'; Zend_Loader::registerAutoload();
As of 1.8 this will produce a deprecation warning, the easiest replacement is this:
include 'Zend/Loader/Autoloader.php'; Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true);
Zend Framework Performance Architecture Part 3: Denormalising Data
by Steven Brown on Jun.12, 2009, under Performance, PHP, Zend Framework
If you’re not already indexing your databases, head on over to http://hackmysql.com/documents and read everything there.
If you’re really looking to squeeze every last drop of performance out of your database then you can denormalise your data where otherwise you would need a join. (continue reading…)
Zend Framework Performance Architecture Part 2: Multiple Row Caching
by Steven Brown on Jun.10, 2009, under Performance, PHP, Zend Framework
In the last article I covered how to precache individual rows from a database in order to improve the performance of your application. While this is certainly useful for instances where we need just a single row, a lot of our queries are to retrieve multiple rows from the database. One of the problems with caching results from these queries is that if a single row’s details change, we need to scrap the entire cache and start again, otherwise we might display outdated information. This applies not only to the fields we are using in the query, but also fields we display to the user. (continue reading…)
Zend Framework Performance Architecture Part 1: Row Precaching
by Steven Brown on Jun.09, 2009, under Performance, PHP, Zend Framework
One quick and easy way to take a chunk out of your database usage is to precache data. When this is done effectively you can actually serve up many requests without even connecting to the database at all. One of the simplest implementations I have found involves precaching rows when they are created or updated. Essentially when the data changes you store the changed data in cache so when it is requested it can be loaded from a file instead of the database. We do this because database access is very slow compared to file access. (continue reading…)
Zend_Cache_Backend_File and tag based cleaning
by Steven Brown on Apr.06, 2009, under Zend Framework
Further to my last post about the automatic cleaning, you will come across similar performance issues when you want to clean or invalidate caches based on tags. Since Zend_Cache_Backend_File does not store all tags in a single index file, it must load and check EVERY SINGLE FILE with the same prefix to see if they contain the tags and need to be cleaned. The end result is a major performance hit when you perform this clean if you have too many files with the same prefix.
In my case I was able to swap out the tagging with a simple prefix change.