As we discussed about OOP basic concepts in Previous section. Now let us understand some more concept. If you ar not aware about what is OOP then please read my previous thread. :)
Polymorphism:-It is another important concept for the OOP. The word is taken from a GREEK language which means the ability to take more than one form. In polymorphism a operation may exhibit different behavior in different instances. If you read my previous blog carefully you know about behavior and instances. Now let us understand this polymorphism with help of a simple example:- Let us suppose we have to add two numbers than in case of two numbers the result is a third number which is sum of the two numbers. But what happen in case of strings. In case of two strings we have concatenation the two strings. The process of making an operator to exhibit different behaviors in different instances is known as operator overloading. Adding a property and method to a class:- This is very easy to add a new propety to our class we simply have to declare a variable inside the class to hold the value of property. The same is done for the method except in adding a method we have to declare a function. Example for this is:-
<?php
class demo{ public $prop; //we are adding a property here. function getdata() { print "hello $this->prop is new property!!"; } } $obj1=new demo(); $obj2=new demo(); $obj1->prop='cyberpirate'; $obj1->getdata(); $obj2->prop='iexploit'; $obj2->getdata();
?>
Now in this code two new things come into play in this statement 'print "hello $this->prop is new property"'. Now you guys may be thinking that what is this bullshit here in this line. Let us understand it here -> is an operator which is used to access the method and property. If anyone familier with c++ then he knew about dot operator which is used to access the property of the class. Here -> this operator is doing the same thing. And the another is variable $this it is used to refer to the current instances.Like in the above example we used two objects but we dont know in advance what is the name of the objects thats why we used $this variable because $this variable allows each object to access its own properties and methods whiout having to know the name of the variable that represents in the exterior program. We never use a $ sign in front of a property when we have to access this property using $this and ->. Note:- We used three keyword to protect our data from the external world. These are public, private and protected. Let us understand these one by one:- public:- This keyword is used to declare a property as a public property which means you can access this property outside the class. One example of this keyword is given above. private:- This keyword is used to declare a property as a private property which means we can not access this type of property outside the class. Now change in above example use private in place of public and then see what happens. protected:-This keyword is used to declare a property as a protected property which means we can not access this type of property outside the class. But we can access it with derived objects or in other words we can say that in inheriting class.
Constructors:- A keyword _construct is used in constructor method. It is automatically used to perform various initialization such as property initialization. Example:-
<?php
class demo { public $name; public function _construct($name) { //construct function to initialize $this->name=$name; // initialization of data } function getdata () { echo "Hello $this->name"; } } $obj1=new demo(); $obj1->_construct('iexploit'); $obj1->getdata();
?> Destructors:- This is used to delete a object or remove it from the system memory when a request is completed running. To do so just create a function called _destruct with no parameters. Before destroying an object it automatically save the data. Suppose we are working with the database then before destroying an object for this database it update the database if changes are made. It means if any properties of the object is changed, they are automatically saved backt to the database when object is destroyed.
I think this sufficient for this section. In the section I am going to show you guys a simple calculator application with OOP approach..:).. Thanks again. And feel free to comment your comment will encourage me to post more threads for this section..:)