Thursday, February 15, 2018

What is python Programming Languages

What is  python Programming Languages

Read more Languages:-

As you might have already known, Python is basically a high level programming language. A programming language is considered high level if it is machine independent and uses human languages like English to specify its syntax, so that it can be easily understood by a human. The programmers just need to deal with the English-like syntax and does not have to worry about the underlying machine code. Although there are many high level languages available in the market, Python has become widely popular among software developers for its clear-cut syntax and easy readability. The lines of actual code in Python have the look of a pseudo-code and hence are easily understandable.

Why is the readability of a programming language so important in software development? It is because; more time is spent by the programmers in reading the lines of code than writing it. Python makes this task easier by laying huge emphasis on code readability. Also, Python can perform a given task using lesser number of lines of code when compared to other high level languages like Java or C++.

 Python can be described as follows:-

 Python is a free, open source, general purpose and object oriented programming language.

Since Python is a free and open source software, its source code can be freely accessed, modified and reused. It is object oriented, which means it involves the usage of classes and objects in programming. It is used for developing applications in a wide variety of domains, making it a ‘general-purpose’ programming language. It has a potential number of users in the software community.

Python was initially developed by Guido Van Rossum, from 1985-1995. Python reduced many complexities of application development because it used English key words, which are easily understood unlike other languages, which require a lot of punctuations. Adding to it is the minimum requirement of syntaxes to be developed.

The following can be said about Python:-

◆  Python is interpreted:-

This indicates that the processing is done by an interpreter at run time and one need not compile one’s program before it being executed. This is closely related to the various languages like PHP Languages and PERL Languages.

◆  Python is Interactive:-

This means, you can directly interact with the Python prompt and execute the programs.

◆  Python is Object-Oriented:-

This indicates that Python deals with the object oriented programming i.e. we can encapsulate the code within the created objects.

◆   Language for beginners:-

Python is a good language to start off with for beginners and programmers who have just started with coding. It also extends its support to developing variety of games related to both text and web.

Python Features:-

◆  Easy-to-learn:-

Python has simple structure, lesser keywords and a well-defined syntax, which allows the learner to understand it in a lesser period of time. Easy-to-read: The code is well defined, as it requires minimum syntax build.

◆  Easy-to-read :-
Its source code is easy-to-maintain when compared to other languages.

◆   Standard library:-

One of its biggest strengths is that it has got a bulk library that is portable and is compatible on various Operating Systems like UNIX, Macintosh and Windows.

◆  Interactive Mode:-

Python supports an interactive mode where results can be entered from a terminal system and can debug the code using this mode.

◆  Portable:-

Python gives the same User Interface when it is run on various hardware platforms.

◆  Extendable:-

Adding up of lower level sections to the Python interpreter is possible. These low-level modules help programmers to use customized tools to increase their efficiency.

◆    Databases:-

It provides connection with major databases available in market.

◆   GUI Programming:-

Python supports graphical user interface that can be developed and for different libraries, system calls, and Operating Systems, like Macintosh, Windows MFC, and X Window system of Unix.

◆   Scalable:-

Python provides a simple and a better structure and supports larger programs than other scripting languages like Shell.

Python as a Scripting Language:-

Python’s robust features as a programming language helped it to prove its versatility by standing out as a scripting language too.

Scripting Languages are nothing but programming languages, which are mostly used for controlling software applications. They support scripts or programs that are only written for runtime operations. ‘Scripts’ do not get compiled but get interpreted; whereas the applications controlled by them are usually compiled. Thus, scripts are different from the core application programs. Scripts and application programs are usually written using different languages. Usually, controlling of the application program by the script occurs by embedding the script into the application program.

Benefits of using Python:-

●. Being a dynamic programming language, Python can exhibit certain behaviors during runtime, which the static programming languages can exhibit only during the compile time.

●.  The variable type in Python can be quickly changed without any fuss or errors. It is not possible to do the same in case of static programming languages as the variable type cannot be changed during the progression of the program. It can be easily integrated with other technologies.

●.  Python has been designed such that it can be easily embedded into other technologies. The productivity of the programmers greatly increases while working with Python, because of its simplicity, readability and support libraries that are easy to use.

●.   Web services can be easily developed using Python. Python is an ideal programming language to work with, if the projects undertaken are big or complex, whose requirements change often. Scientific applications make use of tools like numeric Python and Scientific Python.

●.   In general, an ideal programming language for scientific research is the one that allows notepad or paper scribbling to be directly translated into a piece of code. Python does the job with much √©lan as it looks more like an easy to read pseudo code.

●.    Python is free software and can be freely used for commercial purposes too. Read More

Friday, February 9, 2018

Introducing to Python Programming Languages

Introducing to  Python Programming Languages

Introducing to  Python Programming Languages

Learn More Languages

Even though the foundation for computer programming has been laid by mathematicians in as early as the 1800’s, it has taken nearly hundred years thereafter, for a proper programming language to emerge. Since then, programming languages have evolved from complicated low level instructions to sophisticated, easy to read high level languages. Programming resulted in the development of software systems, which revolutionized the field of computer science. 

Software can be thought of as a technical literature, which can be read by both computers and humans, but not in the same form. Humans can easily understand software only if it is written in a human readable language like English. On the other hand, computers can only understand instructions, which are in the form of machine code (0’s and 1’s). So, certain programs (compilers and interpreters) that can translate high level code to machine code have been developed. 

Even though high-level languages are easy to understand, most of them have complicated syntaxes, which make writing and modifying large programs cumbersome. Hence, high-level languages with simple, clear-cut syntaxes have been released and Python is one such language. Because of its simple syntax, high readability and various other innovative features, programmers all over the world are growing fond of Python. In the recent times, because of its simplicity, several universities have adopted Python to teach introductory computer programming to students. 

Thus, Python is not only getting popular among software developers, but is also gaining popularity as an ideal language to start learning computer programming. If you are new to programming, this book is an ideal place to start learning how to write code. If you are well acquainted with programming, this blog will be an easy read to you and will make you understand what sets Python apart from other programming languages. Read More

Wednesday, February 7, 2018

Conversion specifiers in C Languages

Conversion specifiers in C Languages
Conversion specifiers in C Languages 

There is a specific set of conversion specifiers for printf and another specific set of conversion specifiers for scanf. You have to keep in mind that the conversion characters used for scanf are not the same as those used for printf. Hence, you have to be careful when using them in your program. See to it that you become familiar with the long types.

The following are the conversion specifiers for printf:-

You have to take note that the lowercase letter l may be used as a prefix for long types.

u – it means unsigned denary integer

d – it means signed denary integer

o – it means octal integer

x – it means hexadecimal integer

s – it means string

g – it means use e or f, whichever one is shorter

f – it means a fixed decimal floating point

c – it means a single character

e – it means scientific notation floating point

The following are the conversion specifiers for scanf: -

ld – it means long int

d – it means denary integer

o – it means octal integer

x – it means hexadecimal integer

f – it means float type

h – it means short integer

lf – it means double or long float

e – it means float type

c – it means single character

le – it means double

s – it means character string   READ MORE

Saturday, February 3, 2018

PHP Get & Post Method Tutorial Learne

PHP Get & Post Method Tutorial Learne
Get & post method

The client browser uses two methods to send information from the client to the web server. These methods are GET and POST. However, before sending the information, the browser first encodes the information using the coding scheme called URL encoding. In this coding scheme, key-value pairs, which are here name-value pairs, are combined together in such a way that different name-value pairs are separated by & (ampersand). Any whitespaces are replaced by the plus sign (+) and non-alphanumeric characters are replaced with their hexadecimal values. Once the encoding process is over, the data is sent to the web server.

The GET Method 

When the GET method is used for sending information to the web server, it send the same along with the page URL. The URL is appended by the encoded information and the question mark sign (?) separates the former from the latter. Some of the key things to note about the use of this method include –

●    Since this method appends the page URL, the sent information will also appear in logs of the server.

●       The maximum number of characters that can be sent using the GET method is 1024 characters.

●           There is an inherent issue of compromised confidentiality with this method. Therefore, its use must be avoided when you are sending passwords or critical information to the web server.

●             Another inherent limitation of the GET method is that documents and images cannot be sent to the server using this method.

●             Besides the above-mentioned, it is also possible to access the data sent using this method by retrieving the QUERY_STRING environment variable. Therefore, data is not safe.

●             It is possible to access all the sent information using this method by accessing $_GET, which an associative array provided by PHP for this purpose.

You can try the sample code given below to understand how this method is used.


if( $_GET["name"] || 

$_GET["age"] ) 

echo "Welcome ". $_GET['name']. "<br />"; 

echo "You are ". $_GET['age']. " years old."; 





<form action = "<?php $_PHP_SELF ?>" method = "GET"> 

Name: <input type = "text" name = "name" /> 

Age: <input type = "text" name = "age" /> 

<input type = "submit" /> 




The POST Method 

An alternative to the GET method is POST method. This method makes use of HTTP headers to transfer information from the client to the server. The encoded information is put into the header. Some of the things that you must know about the use of this method for information transfer has been listed below –

●   Unlike GET method, there is no limitation on the number of characters that can be sent using this method.

●    It is possible to send binary as well as ASCII data using this method.

●     As far as the security of data is concerned, data security totally lies in the hands of the HTTP protocol. You can ensure higher security of data by using HTTP Secure.

●     All the data using the POST method can be accessed using the $_POST associative array.

You can try the sample code given below to understand how this method is used.


if( $_POST["name"] || 

$_POST["age"] ) { if (preg_match("/[^A-Za-z'-]/",$_POST['name'] )) 

die ("Name is invalid!"); 

echo "Welcome ". $_POST['name']. "<br />"; 

echo "You are ". $_POST['age']. " years old."; 





<form action = "<?php $_PHP_SELF ?>" method = "POST"> 

Name: <input type = "text" name = "name" /> 

Age: <input type = "text" name = "age" /> 

<input type = "submit" /> 




The $_REQUEST Variable 

The contents of $_GET and $_POST are available in $_REQUEST. Besides this, $_REQUEST also contains data associated with $_COOKIE. The form data sent to the web server using both the methods namely GET and POST can be accessed by accessing this variable. Here $_PHP_SELF variable contains the name of self script in which it is being called.

Thursday, February 1, 2018

Access Control Modifiers in PHP

Access Modifiers 

Access Modifiers are keywords that control visibility of class properties and methods. In PHP there are three of them:

1. Private 

2. Protected 

3. Public 

Let’s describe Mindi in terms of her own class and let’s say that Mindi has a tattoo on her wrist. We could put it in PHP terms like this:

When we do it this way, the tattoo is public by default. That means it can be seen anywhere in the program. It's the same as writing it as:

Class mindi {

Public $ tattoo_location

This means class Billy or class Alex or class Dad can all see Mindi's tattoo because it has public visibility which is set by a public access modifier.

Now let's say Mindi instead has a tattoo that she doesn't want class mom or class dad to know about but wants to show off to her friends. She has this tattoo on her back so she can display it wearing a bikini top.

Class mindi {

Protected $tattoo_location;

Protected function setlocation () {}


Now Mindi can wear her bikini top around her friends and not her mom or dad. We can describe this in PHP by extending an invitation to the people she wants to show it to by using the extends key word:

Since all of these classes extend class Mindi, they can see her tattoo. This is inheritance and all of these classes that extend Mindi are part of her inheritance hierarchy. Now let's say she has her tattoo in a more intimate place (use your imagination) and she only wants her boyfriend Billy to see it and no one else. In PHP terms:

Class mindi {

Private tattoo_location;

Private setlocation ( ) { }

Private letbillysee ( ) { }


So to summarize the three PHP access Modifiers in a more formal way:


restricts the access to the class itself. Only methods that are part of the same class can access private members. Let’s look at an example below:

The reason why data members are declared private is to avoid the outside programs to unintentionally modify the values without necessary validation. If you make a data member private you should provide public getter/setter methods to access the value of this data member. The access modifier


Allows the class itself and all of it’s subclasses to access the data members and member functions. Global access is denied.


means that any code can access the member by its name.

Now back to my previous point about object-oriented principles being “global” in the way a developer should think of them. I used Mindi's tattoo to explain access modifiers but I also had to demonstrate encapsulation and inheritance in order to do so. Inheritance and Encapsulation are really just ways of regulating the ability of an object defined that is defined by a class.

Wednesday, January 31, 2018

PHP Array tutorials with example


One of the most useful data structures supported by PHP is arrays. An array is a collection of data elements that are of the same type. For instance, if you wish to store 20 integers, then it can be stored in an array of 20 elements. You can also look at arrays as a collection of variables of the same type; each of which is capable of storing data.

PHP supports three types of arrays, which include  :- 

•   numeric arrays, 
•   multi-dimensional arrays  
•   associative arrays. 

The individual elements of the array can be accessed using the index or position of the element in the array. The three types of arrays differ in the manner their elements are accessed. Numeric arrays have numeric index that are incremented in a linear fashion. For instance, the first element of the array has the default index of 0. For all elements succeeding this data value, the index is incremented by 1.

            On the other hand, associative arrays have strings as index. The array is composed in a key-value fashion and the keys are stored as index while the values are accessed using the keys. Therefore, the storage of elements and their accessibility is dependent upon the association between the key and value.

Lastly, an array can be composed of elements each of which is an array in itself. Such an array is 2-dimensional. Correspondingly, the dimensionality of an array can be increased depending on the programming requirement. Arrays that have multiple dimensions are called multi-dimensional arrays. Moreover, the elements of such arrays are accessed using multiple indices.

◆   Numeric Array 

Numeric arrays can store any type of data like strings, numbers and objects. However, the only thing worth remembering is that the array’s indices are numeric. Moreover, the default index of the first element of the array is zero. The function used to create arrays is array(). Sample code to demonstrate the functionality of numeric arrays is given below for your reference.

◆   Associative Arrays 

Associative arrays are similar to numeric arrays in the way they function. However, their indexing mechanism is different. They work on the key-value format. Values have keys, which are The associative arrays are very similar to numeric arrays in term of functionality but they are different in terms of their index. Associative array will have their index as string so that you can establish a strong association between key and values.

For instance, if you need a data structure to store the salaries of employees, then the same can best be done using an associative array. In this case, employee names can be used as keys and salaries as values. Please note that you must never put associative arrays inside double quotes as they don’t return any value in that case. Sample code to demonstrate the functioning of associative arrays is given below –




/*Creating associative array using method 1*/ 

$salary = array('james' => 20000, 'jacob' => 15000, 'maria' => 7500); echo "Salary of first employee is ". 

$salary['james'] . "<br />"; echo "Salary of second employee is ". 

$salary['jacob']. "<br />"; echo "Salary of third employee is ".  

$salary['maria']. "<br />"; 

/*Creating associative array using method 2*/ 

$salary['james'] = "high"; 

$salary['jacob'] = "medium"; $salary['maria'] = "low"; 

echo "Salary of first employee is ". $salary['james'] . "<br />"; 

echo "Salary of second employee is ". $salary['jacob']. "<br />"; 

echo "Salary of third employee is ". $salary['maria']. "<br />"; 




◆   Multi-dimensional

Arrays In a multi-dimensional array, every element of the array is also an array. Therefore, a multi-dimensional array is composed arrays, which in turn can also be made up of arrays. In order to access elements of a multi-dimensional array, multiple indices must be used. Sample code to demonstrate the functioning of multi-dimensional arrays is given below –

Monday, January 29, 2018

Decision Making in PHP

Making Decisions

Like most programming languages, PHP lets you write code that can make decisions based on the result of an expression. This allows you to do things like test if a variable matches a particular value, or if a string of text is of a certain length. In essence, if you can create a test in the form of an expression that evaluates to either true or false, you can use that test to make decisions in your code.

You studied expressions, but you might like to quickly review the “Operators and Expressions”section in that chapter to give yourself an idea of the kinds of expressions you can create. You can see that, thanks to the wide range of operators available in PHP, you can construct some pretty complex expressions. This means that you can use almost any test as the basis for decision-making in your code.

PHP gives you a number of statements that you can use to make decisions:

•  The if statement
•  The else and elseif statements
•  The switch statement

You explore each of these statements in the coming sections.

◆  Simple Decisions with the if Statement

The easiest decision-making statement to understand is the if statement. The basic form of an if construct is as follows:

if ( expression )

// Run this code

// More code here

If the expression inside the parentheses evaluates to true, the code between the braces is run. If the expression evaluates to false, the code between the braces is skipped. That’s really all there is to it.

        It’s worth pointing out that any code following the closing brace is always run, regardless of the result of the test. So in the preceding example, if expression evaluates to true, both the Run this code and More code here lines are executed; if expression evaluates to false, Run this code is skipped but More code here is still run.

Here’s a simple real-world example:

$widgets = 23; 

if ( $widgets == 23 )

echo "We have exactly 23 widgets in stock!";


The first line of the script creates a variable, $widgets, and sets its value to 23. Then an if statement uses the == operator to check if the value stored in $widgets does indeed equal 23. If it does —and it should! —the expression evaluates to true and the script displays the message: “We have exactly 23 widgets in stock!”If $widgets doesn’t hold the value 23, the code between the parentheses —that is, the echo() statement —is skipped. (You can test this for yourself by changing the value in the first line of code and re-running the example.)

 Here’s another example that uses the >= (greater than or equal) and <= (less than or equal) comparison operators, as well as the && (and) logical operator:

$widgets = 23; 

if ( $widgets >= 10 && $widgets <= 20 )

echo "We have between 10 and 20 widgets in stock.";


This example is similar to the previous one, but the test expression inside the parentheses is slightly more complex. If the value stored in $widgets is greater than or equal to 10, and it’s also less than or equal to 20, the expression evaluates to true and the message “We have between 10 and 20 widgets in stock.”is displayed. If either of the comparison operations evaluates to false, the overall expression also evaluates to false, the echo() statement is skipped, and nothing is displayed.

The key point to remember is that, no matter how complex your test expression is, if the whole expression evaluates to true the code inside the braces is run; otherwise the code inside the braces is skipped and execution continues with the first line of code after the closing brace. You can have as many lines of code between the braces as you like, and the code can do anything, such as display something in the browser, call a function, or even exit the script. In fact, here’s the previous example rewritten to use an if statement inside another if statement:

The code block between the braces of the first if statement is itself another if statement. The first if statement runs its code block if $widgets >= 10, whereas the inner if statement runs its code block —the echo() statement —if $widgets <= 20. Because both if expressions need to evaluate to true for the echo() statement to run, the end result is the same as the previous example.

If you only have one line of code between the braces you can, in fact, omit the braces altogether:

$widgets = 23;

if ( $widgets == 23 ) 

echo "We have exactly 23 widgets in stock!";

However, if you do this, take care to add braces if you later add additional lines of code to the code block. Otherwise, your code will not run as expected!

◆   Providing an Alternative Choice with the else Statement :- 

As you’ve seen, the if statement allows you to run a block of code if an expression evaluates to true. If the expression evaluates to false, the code is skipped.

You can enhance this decision-making process by adding an else statement to an if construction. This lets you run one block of code if an expression is true, and a different block of code if the expression is false.


if ( $widgets >= 10 )


echo "We have plenty of widgets in stock."; } else


 echo "Less than 10 widgets left. Time to order some more!";


If $widgets is greater than or equal to 10, the first code block is run, and the message “We have plenty of widgets in stock.”is displayed. However, if $widgets is less than 10, the second code block is run, and the visitor sees the message: “Less than 10 widgets left. Time to order some more!”

You can even combine the else statement with another if statement to make as many alternative choices as you like:

◆   The switch statements :- 

The switch statement is a very useful programming construct in scenarios where a single variable is to be tested for different values. The same programming scenario can be implemented using nested if statements. However, the switch is a more efficient option for this as it creates a table of values and according to the value received, the execution directly jumps to the code block corresponding to that value.The syntax for using the switch statement is as follows –

switch (expression){ case value1: -

Code block if the expression evaluates to a value equal to value1;


case value2: -

Code block if the expression evaluates to a value equal to value2; break;


Code block if the expression evaluates to a value that is not equal to value1 or value2;       }

Sample code that implements this concept has been given below for your reference. Read More

Friday, January 26, 2018

PHP comparison operators

Basic operators

Any computer language uses operators to calculate or compare variables, most of which are self explanatory.


PHP uses + (plus),
 - (minus), 
* (multiplication), 
/ (division), 
%( modulo) and "=" the assignment operator.

Let's discuss the modulo operator. The modulo operator is used in finding the resultant remainder after one number has been divided into another. The following are a couple of examples to help you better understand:

The first example is 15 % 5 = 0. As you can see, 5 divides evenly into 15 with no remainder, so the result we get is 0. Now let's use 10 % 7 = 3 as our second example. Because 7 divides into 10 once and leaves a remainder of 3, our answer must be 3.

30 % 6 = 0 (30 - 5 * 6 = 30 30 - 30 = 0)
30 % 7 = 2 (30 - 4 * 7 = 30 - 28 = 2)
30 % 8 = 6 (30 - 3 * 8 = 30 - 24 = 6)

Comparison operators

◆.   $ a = = $ b Equal TRUE if $ a is equal to $ b after type adjustment.

◆.    $ a = = = $ b Identical TRUE if $ a is equal to $ b, and they are of the same type.

◆.   $ a != $ b Not equal TRUE if $ a is not equal to $ b after type adjustment.

◆.  $ a < > $ b Not equal TRUE if $ a is not equal to $ b after type adjustment.

◆.  $ a != = $ b Not identical TRUE if $ a is not equal to $ b, or they are not of the same type.

◆.   $ a < $ b Less than TRUE if $ a is strictly less than $ b.

◆.  $ a > $ b Greater than TRUE if $ a is strictly greater than $ b.

◆.    $ a < = $ b Less than or equal to TRUE if $ a is less than or equal to $ b.

◆.    $ a > = $ b Greater than or equal to TRUE if $ a is greater than or equal to $ b.

Let's see an example with = = = identical operator :-


a = b

a and b are not identical

$ a and $ b are equal because they equal 10, but they are not identical because $ a is a string and $ b is an integer.

$ a = 9; Assignment: $ a equals 9.

$ a + = 7; Addition-assignment: $ a equals $ a plus 7.

$ a *= 4; Multiplication-assignment: $ a equals $ a multiplied by 4.

$ a /= 3; Division-assignment: $ a equals $ a divided by 3.

$ a .= 6; Concatenation-assignment: $ a equals $ a concatenated with 6

Let's write a simple code converting Fahrenheit to Celsius:-

Output: -

F = 80

C = 26.6666666667

Please pay attention to "F =". $ F." < br >" on the code line echo; Here we concatenate 3 separate strings into one.

"F ="

$ F

"< br >" –HTML break.

Two dots are placed between our three strings because dots are used to concatenate strings in

PHP: "F =" $ F "< br >";

The fact is that $ F is not a string. It is an integer because it is equal to 80. If $ F were equal to "80" it would be a string.

When we concatenate the $ F variable between 2 strings, because PHP converts it to a string, there is no need to do anything further.

As I wrote earlier, a string of text can be enclosed in single quotation marks:

echo 'F ='. $ F.' < br >';

To output a string of text, you can use the "print" command instead of the "echo" command.

However, in this case, you would use parentheses and single or double quotation marks.

Print(' F ='. $ F.' < br />');


print(" F =". $ F." < br />");

PHP variables are case sensitive, so $ F is not the same as $ f, and $ lastname is not the same as $ Lastname or $ LASTNAME.

Let us say that we wish to use PHP to output a web page to a browser using the following HTML link:

< a href =" http://" > yahoo </ a >

If we write Print(" < a href =" http://" > yanhoo </ a >");

We would receive an error message due to the use of double quotation marks in our HTML code. To avoid the error message, use single quotation marks for the print command as I've done in the following:-

print(' < a href =" http://" > yanhoo </ a >');

The same is true for an HTML input element:

echo '< input type =" text" name =" lname" value =" James"/ >';


print(' < input type =" text" name =" fname" value =" John"/ >');

One handy feature of PHP code is the ability to include one file inside another. The creation of a website solely in HTML requires you to edit every single page of that website if you later wish to change the navigation, logo or footer. The use of PHP to create the web pages allows you to include one file with navigation or logo in every web page requiring you to edit only one file to later change the navigation or logo.

There are two commands to include the file in your PHP code:

Include(' top_menu.php');


Required(" footer.php");

The difference between the two is, when using the "include" command, if the file to be included does not exist, you will be given a warning message; however, the execution of the rest of the PHP code will not be interrupted. Conversely, when using the "required" command, if the required file is not found, a fatal error message is displayed and the execution of the PHP code will cease.Read More 

Thursday, January 25, 2018

Types of Inheritance in PHP


Recall Mindi’s tattoos, specifically the tattoo on her back? The tattoo on her back was described as protected in PHP terms meaning that it was visible within the Mindi class and to classes that extended the Mindi class. To inherit in PHP5, you should use the keyword extends in the class definition.

Class parent


Class child extends parent



Inheritance passes "knowledge" down. It is a structured way of extending code and functionality into new programs and packages. Classes are created in hierarchies and inheritance allows the structure and methods in one class to be passed down the hierarchy. That means less programming is required when adding functions to complex systems. If a step is added at the bottom of a hierarchy, then only the processing and data associated with that unique step needs to be added. Everything else about that step is inherited. The ability to reuse existing objects is considered a major advantage of object oriented programming.

◆.    Method Overriding 

Method overriding is when the function of base class is re-defined with the same name, function signature and access modifier (either public or protected) of the derived class.

The reason to override a method is to provide additional functionality over and above what has been defined in the base class. Imagine that you have a class by the name of Beverage from which you derive two child classes, Martini and Scotch. The Beverage class has methods defined to stir, mix, etc, but each of the specialized classes Martini and Scotch will have its own style of mixing and stirring and hence would need to override the stir and mix functionality.

Lets look at an example with Beverage:

◆.   Invoking Parent Methods 

First of all, “invoke” means to call, to request the action of something. I just wanted to get that brief explanation out of the way. Now, when you override a method of the base class, its functionality is completely hidden unless it has been explicitly invoked from the child class. To invoke a parent class method you should use the keyword parent followed by the scope resolution operator “:: “ followed by the name of the method as mentioned below:

Parent : : function_name();

Look at the example below: 

In the above example, look at the way in which the showData() function in the Employee child class is invoking the Person parent class’s showData() function. When the program executes the showData() method if the Employee class is called which in turn calls the showData() function of the parent class. After the parent class’s showData() function completes its execution the remaining code in showData() function of the Employee class is executed.

◆.   Horizontal Inheritance – Using Traits 

PHP does not support multiple inheritance; a class can only extend a single base class. What happens if you need code from more than one class? Until recently, developers had to accomplish this in a crude manner by copying and pasting code from additional classes that was needed outside of its base class. Now this can be done by way of a new feature introduced in PHP5.4 called a trait that provides a means of horizontal inheritance.

A trait looks like a class in the way it is structured but it cannot be instantiated. Properties and methods can be defined within a trait structure but they can only be used by a class that incorporates them with the use keyword which is called within the class that uses it, that is, it goes inside the curly braces. It isn’t called in the class definition outside the curly braces like interfaces and the extending class.

◆.    Encapsulation

Encapsulation means what the term implies, to put in a “capsule,” to place functionality into a single package. Consider an object as a capsule or a box that has things in it. The access modifiers are like windows that control who gets to see inside with ‘public’ access being a wide open window that let's everyone see inside and ‘private’ access being fully shaded that doesn't let anyone see inside.

The control over the visibility of the contents of an object is a big part of how encapsulation works. The contents of an object are properties and methods and because they are all packaged inside an object they make up a sort of mini-program that has control over how visible its contents, data, are. This is encapsulation.

Technically speaking, encapsulation refers to the creation of self-contained modules that bind processing functions to the data. Encapsulation ensures good code modularity, which keeps routines separate and less prone to conflict with each other. To reiterate, you can think of each object as a mini program that handles it’s own responsibilities as it’s own self contained capsule or pill.

◆.    Polymorphism

Now that you know that a class is a blueprint and that an object is constructed based on that class it is important to understand that many different objects can be created from the same class. I may have a single class for a table but I can create many different kinds of table objects from that class.

I could have a round table, a square table, a long table a short table. I could have a blue table or a brown table. I could have a table made of oak or one made from plastic; all from the same base class.

Polymorphism simply means many forms. In computer programming, polymorphism is the ability to create a variable, a function, or an object that has more than one form in order to provide capability of adapting to what needs to be done. It is a mechanism by which objects of different types can process data through a single interface. Polymorphism is used to make applications more modular and extensible. Instead of messy, hard to read conditional statements describing different courses of action, you create interchangeable objects that you select based on what you need. That is the fundamental goal of polymorphism.

Polymorphism is a mechanism by which objects of different types can process data through a single interface. Using a typical illustrative example, a program might define a type of object called Animal, containing a function called speak() and, through inheritance, derive object sub-types such as Cow, Dog, etc. that each have their own specific implementation of the speak() function. The mechanism of polymorphism allows such a program to call the speak() function without actually knowing whether it is calling it for a Cow, Dog, or any other derivative Animal, while executing the correct function for the specific type of Animal.

Polymorphism is not the same as method overloading or method overriding (in OOP, a method is a function that belongs to a class, while the class variables are referred to as its members) and should not be confused with these. Read More