Archive for the 'PHP Development' Category

PHP Development

Coding Standards - OMG why…

So I have found my newest, “omg why would you do this” piece of code. The code in question is:
"value" == $variable;
//instead of the traditional
$variable == "value";

The reason given for writing it this way…. because if you accidentally forget to type the second =’s sign it will not cause the variable to set, it will just not set the variable.

WTF

So code backwards to allow you to screw up code? Honestly, check your code you lazy people. When I see this in my current employers code, I change it. When the other developers question me on why I change it, of course I go off on a rant about stupidity, and I was actually told by one, that they would leave it and just conform.

Developers of the world…. please do not conform, and if you decide to conform, do not conform to stupidity. Code like this makes me cry at night when I am trying to sleep.

PHP Development

Zend Framework - Sessions in Memcache

Lets start with, this is working for me and I am posting this because of the time it took me to find the little information I could about Zend_Session::setSaveHandler utilizing memcache.

We are going to make the assumption you have memcache loaded and working; cause I don’t feel like explaining the howto install memcache. We are also going to assume you know a bit about ZF, mainly because I suck at explaining the is, why, and where of programming.

  1. Create a customer session handler that implements Zend_Session_SaveHandler_Interface
  2. In your boot strap load your session handler class, Zend_Loader::loadClass(’Your_SessionHandler_Class’)
  3. In your boot strap Zend_Session::setSaveHandler(new Your_SessionHandler_Class($_cache)); .. more about the $cache var in a sec
  4. Zend_Session::start(); and your good to go

Steps By Example:

Step 1.

class Lib_Custom_Helper_SessionHandler implements Zend_Session_SaveHandler_Interface {
private $maxlifetime = 3600;
public $cache = '';
public function __construct($cacheHandler) {
$this->cache = $cacheHandler;
}
public function open($save_path, $name) {
return true;
}
public function close() {
return true;
}
public function read($id) {
if(!($data = $this->cache->load($id))) {
return '';
}
else {
return $data;
}
}
public function write($id, $sessionData) {
$this->cache->save($sessionData, $id, array(), $this->maxlifetime);
return true;
}
public function destroy($id) {
$this->cache->remove($id);
return true;
}
public function gc($notusedformemcache) {
return true;
}
}

Step 2:
Zend_Loader::loadClass('Lib_Custom_Helper_SessionHandler');

Step 3:
Ok, so after some reading I saw a few ways to plug memcache into my session handler. I opted for this option because I was already establishing config variables and connection information for Zend_Cache, utilizing memcache, so instead of passing the connection info again, I am passing my sessionhandler my cache handle which feeds directly into memcache as it is. So before my session setup I run
$_cache = Zend_Cache::factory('Core', 'Memcached', $frontendOptions, $backendOptions);
and then
Zend_Session::setSaveHandler(new Lib_Custom_Helper_SessionHandler($_cache));

Step 4:
Zend_Session::start();

And there you have it… my sessions are now stored into memcache via my already running cache handler… Hope this helps someone out there… took me forever to find info on specific howto’s…

PHP Development, Vexed Daily

White Star Relaunched

Well I found myself with some downtime recently and decided to go back to freelance development. I figured it would be best to have some sort of online front facing site. In the past I have always approached my freelance development work from a business aspect, but have decided this time to take a more individual approach.

It still has some minor work to be done, but for the most part it is complete. White Star Studios

PHP Development

Zend Framework - Mysqli - Socket Connections

I found my first real dislike / flaw in the Zend framework; it was easily fixed thanks to a write-up that I found online, but still annoying.

It appears that in the database adapter for mysqli within the ZF the use of a socket for your connection rather than directly from host was not permitted. There is an simple solution that I found at This website, and after making these adjustments to the zend library everything works fine.

The fix is as simple as adding $socket to an array that is then passed to the ZF connection protocol. I am very curious as to why the ZF team left out socket from the connection string for mysqli and not for other database adapters.

Next »