Wednesday, March 26, 2008

OOP Concept

Object-Oriented programming..
* Procedural programming involves viewing a computer program as a sequential list of computational steps / procedures) to be carried out. In procedural programming, the list of procedures can be further broken down into subroutines and functions.
* Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For example you may have a Person object to hold the data related to a person and even provide some functionality that this person may be capable of.
* The term Object that gives OOP it’s name, refers to a conceptual object that represents an item / entity in system.
* attributes - the characteristics of our object.
* methods - a set of functions and calculations that are either performed to modify the object itself, or are involved in some external effect.
* The constructor of a class is a special operation that is run upon instantiation - when an object is created. It cannot have a return type. A properly written constructor should never contain any functionality that could fail, thus leaving the object in an invalid state.
* The destructor of a class is the opposite of the constructor in that it is run when an object is destroyed. It’s function is to clean up and to free the resources which were used by the object during run-time and unlink it from other objects or resources. This should result in the invalidation of any references in the process.

OOP language should have:
* Inheritance
* Polymorphism
* Encapsulation, Abstract data types and information hiding

=> Inheritance : Inheritance allows a (sub)class to inherit all the attributes and methods of a parent class - in effect, extending the parent. It can best be described as an “is a” relationship. For instance, if we had a class of “fruit”, we could extend it by defining classes of “apple”, “orange” and “banana” (apple "is a" fruit)
* Composition is a slightly different sort of relationship - this is where it could be said that a class was “composed” of other classes. For instance, a wall is “composed” of bricks and a molecule is “composed” of atoms. Neither of these examples could be described as inheritance - the statement, “a wall is a brick” simply isn’t true. Composition can be described as “has a” and “uses a” relationships; a wall “has a” brick or a wall “uses a” brick.
* The main difference between two relationships association & aggregation is simple; in association the composing classes are destroyed when the parent is destroyed, however, in aggregation they are not. This ultimately means that, when aggregation is adopted as a relationship, the composing classes and objects can be re-used in other classes and objects within the system.

=> Polymorphism : meaning different classes can have different behaviours for the same attribute or method. Overloading is a good example of polymorphism in action. Polymorphism, at it’s most basic, describes the fact that a given function may have different specifications, depending on the object to which it is applied.
* Late Binding :

=> Encapsulation, Abstract data types and information hiding :

Wednesday, March 5, 2008

number of processes running..

We can replace xyz.lock functionality by counting the number of processes running.
As many of us use some xyz.lock file just to ensure that same process should not run more than one time simultaneously.

I faced a little problem with this concept (lock file) :

Problems (lock file):
1. In case a script dies without deleting the lock file, when second time scripts executes, It checks for the lock file and exits without doing anything assuming that one thread is already running, which is not true.
2. We have to manually delete the file (which can be done only If we locate the problem that script died)

Solution (check process):
1. Instead of using lock file concept, we should use checkProcess (sample code provided). Yes, you can change the code according to your requirement, after all it is open source), which will check If process is running or not, and will work accordingly.
A better practice with this is that, we also ensure to get alert to find the problem too (as we do in the lock file concept too (assuming)).

Benefit :
1. If some script is running in cron very frequently (even if daily), the very next attempt will not fail if the previous attempt fails, and it will help in automating the system more efficiently.

/**
* Function : checkProcess
* Input : name of the process like garbageCollector
* Output : count of the processes running. In case of garbageCollector, it will be always in (0,1) ideally.
* Author : Vishnu Kumar Agarwal
* Description : returns count of the processes currently running
*/
function checkProcess($processName)
{
return $processCount = shell_exec("ps ax | grep $processName | grep -v grep | wc -l");
}

echo "Count",checkProcess("garbageCollector");