rickogden.com

Posts Tagged ‘php’

When Objects Act Like Arrays

I’m a big advocate of object-oriented programming, even though only a few years ago I hated it and thought it was “over engineering”. I don’t believe, however, that object orientation is suitable for every circumstance. Anyway, that’s for another blog post. Objects are great, but not always the best when handling data, particularly things like data collections.

One of the nice things about storing collections of data in arrays rather than objects is the fact that you’re able to easily loop through the collection, and extract data from the collection depending on the position. Sometimes though we may want to manipulate the collection, and store more information than just the data in the collection. This is where SPL comes in.

As of version 5.0 PHP has included the SPL (Standard PHP Library). I am going to look at a couple of interfaces from this library in this blog post: ArrayAccess and SeekableIterator. These interfaces allow us to do a couple of things: SeekableIterator allows us to iterate through the object (eg. a foreach() loop), and the ArrayAccess allows us to extract data from the object as if it was an array. All this in a way that is completely controlled by the programmer.

Definition for the SeekableIterator:

SeekableIterator extends Iterator {
/* Methods */
abstract public void seek ( int $position )

/* Inherited methods */
abstract public mixed Iterator::current ( void )
abstract public scalar Iterator::key ( void )
abstract public void Iterator::next ( void )
abstract public void Iterator::rewind ( void )
abstract public boolean Iterator::valid ( void )
}

Definition for the ArrayAccess:

ArrayAccess {
/* Methods */
abstract public boolean offsetExists ( mixed $offset )
abstract public mixed offsetGet ( mixed $offset )
abstract public void offsetSet ( mixed $offset , mixed $value )
abstract public void offsetUnset ( mixed $offset )
}

The way these work is that they require a variety of methods to appear in the class. With SeekableIterator there is generally a “position” property which is just an integer holding the current position in the collection (so normally defaults to 0). The methods that are required are: seek, current, key, next, rewind and valid. The “seek” moves the position to the number passed in as an argument, “next” method normally increments the position by one, and “rewind” resets the position back to 0. “current” returns the item at which the position property is at, “key” tells you the current value of position and “valid” checks to make sure an item exists at that position. You can then put the object in a foreach loop as if it was an array!

foreach($iterableObject as $item {
     ...
}

ArrayAccess requires fewer methods, and some of them are similar: “offsetExists” is the same as “valid” but rather than getting the position from the internal position property, it is passed in as a parameter. “offsetGet” returns the item at a given position and “offsetSet” sets an item to a given value. You can then treat an object like an array by using standard array notation:

$item = $arrayAccessObject[0];

A single class can implement both of these interfaces without conflict, and SPL contains many more things like this. I have not written this as a tutorial as the PHP Docs are incredibly good on this subject and easy to follow.

http://php.net/manual/en/class.seekableiterator.php
http://php.net/manual/en/class.arrayaccess.php

Installation of Memcache in PHP on Ubuntu/Debian

Happy new year all! Just a quick post about how to install Memcache in PHP on an Ubuntu or Debian server (I think it will apply to Redhat based servers, just substitute the apt-get for yum).

Log on to your server as root (either directly or using sudo su), then install the Memcache daemon through apt:

apt-get install memcached

Once installed you need to install the Memcache module into PHP. This is done through PECL which in turn is installed through PEAR. When they are both installed, you need to run the command:

pecl install memcache

This will install the memcache extension, and once done will tell you to add the the line “extension=memcache.so” into your php.ini file. I prefer keeping these in separate files (so that the php.ini file can be updated). The easy way to do this is:

echo "extension=memcache.so" > /etc/php5/conf.d/memcache.ini

And finally, reload Apache

/etc/init.d/apache2 reload

If successful, Memcache will now appear in your phpinfo().

PHPNW10 – An Overview

I had a fantastic time at phpnw10 this year. Met loads of old friends, and made a few new ones. The main conference kicked off on Saturday morning with a keynote by Lorna Mitchell entitled “Teach a man to fish”. This was about how team training is important with proper feedback mechanisms which enable a developer (and a team) to learn from their work and give them the skills to teach themselves further. Following that I went to see Derick Rethans giving a talk on geolocation within PHP. This gave me a great insight into how geolocation/mapping works online and has given me some food for thought for my MSc. After, I headed back to the main track to see Ian Barber speaking about debugging. I find that this is the sort of talk I can’t get enough of, and discovered a couple of cool new tools to help with debugging on top of the existing ones I already use.

After lunch I saw Michelangelo Van Dam talking about unit testing within ZF. Like everything with ZF, it seemed a lot easier than I thought it might be. Then there was another ZF talk in the same room by Rowan Merewood who, as always, gave a rather entertaining talk. It was about Zend_Acl and used real-world examples (ship classes from Star Trek) to demonstrate how they work.

Scott MacVicar gave a really interesting talk on HipHop for PHP, allowing PHP code to be compiled. He went through the process of using it, what the current limitations are and the plans for the future. As this was Scott, heckling was aplenty, and made this talk very enjoyable! Then it was back to the main track for the final session: The Framework Shootout, a panel session chaired by Marcus Deglos. Although there were no definitive conclusions that came out from the panel, it was very interesting to see different people’s take on things, and the strengths and weaknesses of a variety of frameworks.

Overall it was an excellent event with excellent attendance! However we have a bit of a dilemma now. We have reached the capacity of this venue. This means that if we want to expand we will need to find a new one. If you have any thoughts/ideas/recommendations on not only venues around Manchester, but how we can expand further to increase the impact of the PHPNW group, please let me know! Finally I would like to thank Jeremy Coates and all the helpers on the day, the event ran fantastically smoothly and it was all down to you, thanks everyone!

Why Students Should Attend PHPNW10

PHPNW is primarily a conference aimed at professionals within the industry to allow them to learn from each other and discover new ideas and techniques which they can then apply to their every day work. What is less prominent is how this is equally as useful for students, who may want to work in this same area once they have graduated.

Read the rest of this entry »

Netbeans 6.9 Beta + Zend Framework

Having just got my brand new MacBook Pro, I’ve been setting it up as a development environment (blog post about that to come). I decided to install the new Netbeans 6.9 beta. The main reason for this is the Zend Framework (and Symfony) support.

In the past I have found Netbeans to be pretty good with code-completion when being used with Zend Framework, however with the release of Zend Tool (something I do really like), you’ve had to switch from Netbeans to the command line in order to create the project and then create a new Netbeans project from existing sources. This was a bit of a hassle.

Now, all you need to do is download the Framework, go into the Netbeans preferences > PHP > Zend tab, Zend script box should point to the zf script (from within the bin directory of the ZF downloading). On Mac and Linux it wants the zf.sh file (on Windows it will probably want the zf.bat file, although not tested). Once that has been set up, you can now create brand new Zend Framework projects from within Netbeans, and it preconfigures everything for you. Lovely!

suPHP + Userdir on Ubuntu

Recently I’ve had the need to combine suPHP with the userdir mod for Apache on Ubuntu. By default they don’t play nice together. So here is a quick guide on how to get it working.

If you have installed a standard LAMP server (there are many guides on how to do this), you now need to install the suphp package for apache, this is called libapache2-mod-suphp:

$ sudo apt-get install libapache2-mod-suphp

Once that has been installed open the file /etc/suphp/suphp.conf in your favourite text editor. Find the line that has the docroot on, and change the docroot so that it is just “/”. This means that the suphp engine will parse anywhere in the file system, and not just in the standard html directory, thus allowing users to have their own.

docroot=/

You may also want to change the security options as appropriate, just change the “false” to “true” of the applicable ones the enable them. This is worth experimenting with. Further down you will want to set the “check_vhost_docroot” is set to false, this again is to do with the fact that userdirs are not in the vhost’s document root.

check_vhost_docroot=false

Finally you have the min_uid and min_gid properties. These are worth altering if you still want to be able to have a website running as www-data (such as the default website). If this is the case, change them both to the uid and gid of www-data (33 by default). It is not recommended to allow suphp to run as root, so do not set it to 0.

min_uid=33
min_gid=33

Finally, you need to enable the mods suphp and userdir, and disable the mod php5, this is done with two commands, and then restart apache2:

sudo a2dismod php5
sudo a2enmod userdir suphp
sudo /etc/init.d/apache2 restart

This should then allow you to run php scripts as the user who created them. To test this, create a new php file that contains:

<?php
system(id);

This will give you information of the user that the php process is running as. I recommend changing the ownership and retrying it, just make sure sure suphp is running as it should be.

Happy New Year and Updates

With it being the festive season, I have not done a huge amount of interest recently. I’m hoping to have a few things for you soon. Currently I have been experimenting with Drupal, having never used it (properly) before, this is a real learning curve.

Also I am working on getting suPHP to work with userdir, to allow users of a server to have their own webspace in a secure fashion. I will be blogging about this when it is done.

However, I have found a few things that may be of interest:
Things you probably didn’t know about PHP – Some really interesting and neat things you can do with PHP.

PHP Object Generator – Generates class structures and objects, so you don’t have to

(An Extended) Beginners Guide to Object Orientation in PHP

Those of you who were that the PHPNW09 conference on the Sunday morning may have seen my talk on Object Oriented PHP. At the beginning of the month I gave a talk at the PHPNW user group which was based on the original talk, but slightly extended.

Here are the slides

Talking at PHPNW – 1st December

Just to let you know that at the next PHPNW meeting I will be giving an extended version of my talk: Beginners Guide to Object Orientation in PHP, which I gave at the PHPNW09 conference.

For those who don’t know, this is held in Revolution on Deansgate Locks in Manchester, and starts at 7pm on Tuesday 1st December.

Upcoming link

Zend_Auth Pam Adapter

A while ago for work I wrote a plugin for Zend Framework. It was an adapter for Zend_Auth that authenticated against PAM for authentication of local users through a web interface. So I have decided to release it in the hope that there will be other people out there who find it useful.

Please visit the Zend_Auth Pam Adapter page in the labs section for more information.