Web programming is an aspect of web site development, and the role of web programmer is very significant just as the web designer’s role in the web design aspect of web site development.
Programming languages have developed from machine language to low-level language and then to high-level language. The high-level language, which is a language close to natural language (the language we speak), is written using specific approaches. Notable are the monolithic and structural programming approaches.
With the consistent style, you write a whole program in one single block. In a structured programming approach, a program divided into blocks of codes called modules with each module performing a specific task. BASIC, COBOL, PASCAL, C, and DBASE that ran on the MS-DOS platform could be written using both approaches.
Following the revolution of the windows operating system, it became possible to write programs using a more advanced structured programming approach than the type used on the MS-DOS platform. This is the Object-Oriented Programming (OOP) approach, where a program divided into classes, and each class is subdivided into functions or methods with each function providing a specific service.
C++ and Java are typical examples of Object-Oriented Programming (OOP) languages that were initially developed for non-web solutions. As the preference for web applications grew more and more according to the historical development of the internet and the historical development of the web, the need to improve on scripting languages continued to arise, and one of the ways they embarked on it was by making scripts Object-Oriented. Java applet and PHP (Hypertext Preprocessor) are examples of Object-Oriented Programming (OOP) languages for web solutions.
PHP was initially non-Object-Oriented, but it has been fully upgraded to an Object-Oriented Programming language (OOP), demonstrating the three pillars of Object-Oriented Programming (OOP) – Encapsulation, Inheritance, and Polymorphism. Thus, it is possible to write server-side scripts in an Object-Oriented fashion.
Object-Oriented Programming (OOP) structures program into classes and functions or methods. To use a class and access the services rendered by each function, you must create an instance of the class.
When an instance is created, an object is produced, which is held by an object variable. It is this object that will now be used to access each function and make use of its service. The syntax of class instantiation statements for object creation varies from language to language. In PHP, you use the new keyword.
For instance, if you have a class with name customer and you want to instantiate it and use the object to access function select_records() in the class, you go about it this way-
$cust = new customer();
The first line created an instance of the class customer and an object held by an object variable $cust. The second line accesses the service provided by function select_records() with the object variable $cust.
Java, too, uses the new keyword for object creation, but the application of the keyword in C++ is different where it is used by a pointer variable during dynamic memory allocation. I mentioned earlier the three pillars of Object-Oriented Programming (OOP)-Encapsulation, Inheritance, and Polymorphism. They are the integral features of PHP. Encapsulation is the process of hiding all the details of an object that do not contribute to its essential characteristics. This is achieved by making all instance variables of a class private so that only the member functions of the class can access its private instance variables. Inheritance is a situation in which a class derives a set of attributes and related behavior from a parent class.
The parent class is called the superclass or base class, and the inheriting class is called a subclass. The member variables of the superclass become member variables of the subclass (derived class). In PHP, you use the keyword extends to implement inheritance just like Java, for example,
class customer extends products
Polymorphism is an extension of inheritance. It is a situation when a subclass overrides a function in the superclass. When a function or method is overridden, the name and the signature of the function in the superclass are retained by the overriding function in the subclass, but there is a change in the function code.
Another essential feature of Object-oriented Programming (OOP) language is a constructor. A constructor is a function or method bearing the same name as its class name, and it is used for initialization of member variables and invoked as soon as the class is instantiated, unlike other member functions that are invoked only with the use of the object variable. At this point, let us use the submission of data with, for instance, a fixed asset register form for further illustration. Your PHP script needs to retrieve data posted from the form, connect to a database, print custom error messages, and insert data into the database table. Using the Object-Oriented Programming (OOP) approach, you need four functions in the class-
- The constructor- to retrieve the posted data from the form.
- A function needs to connect to an MySQL database.
- A function to insert records to the database using the INSERT SQL statement.
- A function to print custom error messages.
Because your program is in an organized form, it is easier to understand and debug. This will be highly appreciated when dealing with long and complicated scripts like those incorporating basic stock broking principles. Within the limit of the structured programming capabilities of the non-Object-Oriented Programming languages of BASIC, COBOL, PASCAL, etc., you could organize program too by dividing it into smaller manageable modules. However, they lack the encapsulation, inheritance, and polymorphism capabilities of Object-Oriented Programming (OOP), which demonstrates a great advantage of the Object-Oriented Programming (OOP) approach.