May 2, 2020 | programming | No Comments
Web programming is an aspect of web site development and the role of web programmer is very significant just as web designer’s role in 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 certain approaches. Notable are the monolithic and structural programming approaches. With the monolithic style, you write a whole program in one single block. In structured programming approach, a program is divided into blocks of codes called modules with each module performing a specific task. BASIC, COBOL, PASCAL, C, and DBASE that ran on MS-DOS platform could be written using both approaches.
Following the revolution of windows operating system, it became possible to write programs using a more advanced structured programming approach than the type used on MS-DOS platform. This is the Object-Oriented Programming (OOP) approach where a program is 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 which were originally 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 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 originally non Object-Oriented but it has been fully upgraded to an Object-Oriented Programming language (OOP) demonstrating the 3 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 statement 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 class customer and an object held by 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 super class or base class and the inheriting class is called sub class. The member variables of the super class become member variables of the sub class (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 sub class overrides a function in the super class. When a function or method is overridden, the name and the signature of the function in the super class are retained by the overriding function in the sub class but there is a change in the function code.
Another important feature of Object-oriented Programming (OOP) language is 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 submission of data with, for instance, fixed asset register form for further illustration. Your PHP script needs to retrieve data posted from the form, connect to database, print custom error messages and insert data into the database table. Using the Object-Oriented Programming (OOP) approach, you need 4 functions in the class-
- The constructor- to retrieve the posted data from the form.
- A function to connect to MySQL database.
- A function to insert record 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 complex 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.
Copyrights reserved.Approach, ObjectOriented, OOP, programming, Web