PHP is single-threaded1. Because of this, a single server computer runs as many instances of PHP interpreter as the number of concurrent pages to be processed.
This presents an efficiency problem when connecting to databases. Connections to databases cannot be efficiently pooled. Persistent connections to database servers can be maintained only per process. When using that, you will end up making a large number of connections to the database. I think that a multi-threaded architecture will reduce database load even on *nix operating systems.
PHP community is rather quick to tag this as a database problem. I do not think so. Database scaling is difficult and expensive. It makes sense to get the maximum out of any database system that you have. Therefore, there is a case for making PHP multi-threaded.
1 Many will point out that PHP extensions are the problem in making it multi-threaded. If that is the case, there has not been an effort to tag what works multi-threaded, and what does not.
Tuesday, April 8, 2008
Inheritance in Javascript
If you recall, I had explained the creation of a Timer class in javascript. I had made a promise to explain how to do inheritance and perform timer callback to a class method. Here it is.
Suppose we want to create another class that inherits all the methods and properties of the Timer class. The following code illustrates it.
Now, suppose we want to make the timer callback a method in the Inherited_Timer class. We can do it as follows
Modify the constructor to:
Suppose we want to create another class that inherits all the methods and properties of the Timer class. The following code illustrates it.
function Inherited_Timer()
{
}
Inherited_Timer.prototype = new Timer;
Now, suppose we want to make the timer callback a method in the Inherited_Timer class. We can do it as follows
Inherited_Timer.prototype.on_timer = function ()
{
// write your code here
// the object is accessible using this
};
Modify the constructor to:
function Inherited_Timer(timeout, repeat)
{
var me = this;
this.start(timeout, function () { me.on_timer(); }, true);
}
Labels:
callback,
inheritance,
javascript,
object oriented
Event handlers
Usually, event handlers are defined inline. See the example below:
When the event handler function is called by javascript, the this object is set to the form element firing the event; this does not refer to the object that the event handler function is a member of. Therefore, we store the reference to the object in another variable named me.
Another quirk is that Internet Explorer does not pass an event object into the event handler function like all other browsers do; instead it sets a global window.event variable. The line e = e ? e : window.event; handles this case.
The line this.input_ctrl.onkeypress = me.on_keypress; sets the event handler in javascript.
If you want to encapsulate event handling inside a javascript object, you can implement it as follows.
<input onkeypress="return checkkeys(event)" type="text">
checkkeys(e)
{
return true;
}
function Event_handler_class(input_ctrl)
{
var me = this;
this.input_ctrl = input_ctrl;
this.on_keypress(e)
{
e = e ? e : window.event;
// this code exists due to browser compatibility issues
// but this deals on with the onkeypress event, and
// therefore, not explained here
var keycode = (document.all) ? e.keyCode : e.which;
// add your code based on keycode here
// to access the Event_handler_class, use me
return true;
};
this.input_ctrl.onkeypress = me.on_keypress;
}
When the event handler function is called by javascript, the this object is set to the form element firing the event; this does not refer to the object that the event handler function is a member of. Therefore, we store the reference to the object in another variable named me.
Another quirk is that Internet Explorer does not pass an event object into the event handler function like all other browsers do; instead it sets a global window.event variable. The line e = e ? e : window.event; handles this case.
The line this.input_ctrl.onkeypress = me.on_keypress; sets the event handler in javascript.
Labels:
browser compatibility,
event handler,
javascript,
OOP
Saturday, April 5, 2008
Creating a object oriented Timer in Javascript
I wanted to create a Timer class in javascript. I found that a function definition can be used as a class definition.
Javascript has two asynchronous timer functions - setInterval and setTimeout. Both these functions take two arguments -- the timeout period in milliseconds, and a callback function to be invoked when the timer fires. The only difference: setTimeout fires only once; setInterval fires repeatedly.
The timer needs a start() method so that the timer can be activated. I wrote the following function for that.
Then we add a stop() function to be used to stop a running timer.
Now, how do we use this class? It is very simple.
In another post, I will explain how you can add the Timer functionality to another javascript class, and make the callback into a member function of that class.
// define the class
function Timer()
{
}
// instantiate an object of the class
var myTimer = new Timer();
Javascript has two asynchronous timer functions - setInterval and setTimeout. Both these functions take two arguments -- the timeout period in milliseconds, and a callback function to be invoked when the timer fires. The only difference: setTimeout fires only once; setInterval fires repeatedly.
The timer needs a start() method so that the timer can be activated. I wrote the following function for that.
Any variable that is defined with the prefix this. is a public member. These must be initialized in the constructor, hence we modify the constructor as:
Timer.prototype.start = function(timeout, callback, repeat)
{
this.timeout = timeout;
this.callback = callback;
this.repeat = repeat;
if (this.repeat)
{
this.timerid = setInterval(callback, this.timeout);
}
else
{
this.timerid = setTimeout(callback, this.timeout);
}
}
// define the class
function Timer()
{
this.timeout = 0;
this.callback = null;
this.repeat = false;
this.timerid = 0;
}
Then we add a stop() function to be used to stop a running timer.
Timer.prototype.stop = function()
{
if (this.repeat)
{
clearInterval(this.timerid);
}
else
{
clearTimeout(this.timerid);
}
};
Now, how do we use this class? It is very simple.
var myTimer = new Timer();
myTimer.start(3000, myFunc, true);
function myFunc()
{
// write the timer code here.
}
In another post, I will explain how you can add the Timer functionality to another javascript class, and make the callback into a member function of that class.
Subscribe to:
Posts (Atom)