Snippets Web Programming

Creating an Instance of an Object without Calling Its Constructor in PHP

Today, I stumbled upon an article in a local PHP developers group. It was about creating an instance of an object without calling its constructor (as mentioned in post title).

At first, the idea made no sense to me. Because, constructor method is run every time an object is instantiated, this property is the whole point of writing a constructor. Out of curiosity, I started reading the article. As it turns out, it is really beneficial for testing and mocking purposes. The more interesting thing is the method used to achieve this is simply brilliant.

The trick which originally occurs in PHPUnit source code is as follows:

$object = unserialize(
    sprintf('O:%d:"%s":0:{}', strlen($className), $className)

As of PHP 5.4, there is a built-in method for this in ReflectionClass:

$reflection = new ReflectionClass($className);
$object = $reflection->newInstanceWithoutConstructor();
Snippets Web Programming

VirtualBox, Shared Folders and Cache

I have recently set up a virtual server environment using VirtualBox for quick web development. Using shared folders feature, I wanted to avoid file uploads between VM host and guest. However, when I make request to an updated a file, server responses with the old version.

First, I cleared and disabled my browser cache. The result was the same. Second, I checked the file both on host and guest machines. Both were up to date, so the issue was about the server. Then I have made some experiments with expires headers, again, no luck.

I continued searching on the internet and found this blog post. Sendfile option is disabled by default on Nginx (see documentation), but apparently Ubuntu had turned it on with the configuration file on the package. You can turn it off by setting the flag off or commenting the line:

# sendfile on;
# or
sendfile off;

Sendfile is enabled by default of Apache. In order to turn it off, you can refer to the official documentation here.

Linux Python Programming Web Programming

Managing Gunicorn Processes With Supervisor

Last week, I have written a post about gunicorn applications. We started gunicorn manually, and our application worked. Yay!

However, everything is not so great. When (If) the server reboots, gunicorn must be started manually, again. We should find a way to automate this. Actually, there are few ways to accomplish this, such as: init scripts, and supervisord.

Init scripts are more than enough. But they have some drawbacks. They are not so easy to write or maintain. If you have several projects, things get even harder. Supervisord is a relatively easier way for managing multiple gunicorn processes.

Let’s start with installing supervisor:

sudo apt-get install supervisor

Supervisor uses configuration files for applications located in /etc/supervisor/conf.d/ directory. The configuration for our application is below:

command = gunicorn hello:app
directory = /path/to/hello/
user = username

This is a very basic configuration required to run gunicorn. Since we are using virtualenv, we need to change “command” parameter to use python and gunicorn from our environment instead of global ones.

command = /path/to/virtualenv/bin/python /path/to/virtualenv/bin/gunicorn hello:app

Now we can test, whether our configuration works or does not. Reload supervisor with following commands and start our application. Stop gunicorn if it is running, and start it again with supervisor.

supervisorctl reread
supervisorctl update
supervisorctl start hello

Now gunicorn must be running, you can make sure by visiting page. If server reboots, supervisor starts it. If gunicorn fails, supervisor restarts it. Finally, everything is great!

You can find more information about supervisor configuration parameters in the documentation. You can also read my blog post about restarting and reloading supervisor.

Linux Python Programming Web Programming

How to Run Flask Applications with Nginx Using Gunicorn

We have recently bought a VPS for İTÜ24, the online newsletter of Istanbul Technical University. The server is running on Ubuntu Server 12.04 operating system. Due to limited memory resources and performance concerns, we preferred to setup nginx as web server.

Our server will serve several web pages and applications developed in various programming languages, such as PHP, Python, Ruby (on Rails). Currently, we have one Python application, which is using Flask framework.

How we run Flask application with nginx, step by step…

Snippets Web Programming

HTML5 Video Loop Support for Firefox

You created a fancy HTML5 page with video element but guess what, Firefox 4 does not support video loop.

I found a workaround for this problem with javascript.

<video onended="this.play();" loop />
Web Programming

How to Create Hyperlink DIV Elements

Today, I encountered a problem: making div element a hyperlink. For example:

<a href="http://www.onurguzel.com/"><div>content</div></a>

It works, but SHOULD NOT be used. Because it is against the web law: W3C (X)HTML specifications!

A very basic (and valid) solution:

div a { display: block; height: 100%; }
<div><a href="http://www.onurguzel.com/">content</a></div>

However my case was a little bit more difficult:

<a href="http://www.onurguzel.com/">
	<div id="first">
		<div>some content</div>
		<div>some other content</div>

We already know that is not a valid layout. Because a block element (div) is used in an inline element. If I change layout, a have to insert hyperlink tags (a) in all block elements, and the background of the “first” div element will not be clickable. So here comes the solution: