You may already be familiar with OOP from working with other languages such as Java, C#, or Perl, but if you’re not, a general introduction follows shortly.
The rest of the POST teaches the main concepts of OOP, and shows how to write object – oriented code in PHP. You learn:
How to define classes, which are the blueprints from which objects are made. You then learn how to create objects from classes.
Two important components of objects — properties and methods — and how to use them to add rich functionality to your objects. Along the way you learn how to make your objects as self - contained as possible, which allows them to be readily reused for different purposes.
How to use inheritance — a process where one object inherits behavior from another. This is one of the most powerful aspects of objects. You learn how to achieve this in PHP, and how to fine - tune the inheritance process to create robust classes that you can use again and again.
Other OOP concepts such as abstract classes, interfaces, constructors, and destructors.
Some of PHP’s handy object - related functions for automatically loading classes, converting objects to strings, and identifying an object’s class.
Why we use Object-Oriented Programming
If you write code that passes bulk of data from one function to another function--- and this technique is called procedure programming. While Object- Oriented Programming takes different approach. With the use of object - oriented application is a set of collaborating objects that independently handle certain activities. An object - oriented approach gives you some big benefits over procedural programming.
Advantages of OOP
To work with oop, it has classes which have the same properties and behaviors as the real – world concepts they represent, which helps you to quickly identify what code needs to be written and how different parts of the application need to interact.
Second advantage of OOP is to reuse the code. For example, an application that manages hospital patient records might contain a class called Person. A number of different people are involved in patient care — the patient, the doctors, the nurses, hospital administrators, and so on. By defining a class called Person that encompasses the properties and methods common to all of these people, you can reuse an enormous amount of code in a way that isn’ t always possible in a procedural programming approach.
Class and Object
• As we have studied the variables and structure concept in the c programming language, and we also know that if we are interested to use the structure variable we can use them with the use of structure variable (OBJECT).
• But all the variable declares inside the structure all behave like public, that’s why the class and object came into the picture, which contain the access specifies functionality.
class Harshida_FirstCLass_Example
{
public $FirstName;
public $LastName;
}
//Create Object of Class
$obj_h1=new Harshida_FirstCLass_Example();
//Assign Value
$obj_h1->FirstName="HARSHIDA";
$obj_h1->LastName="PARMAR";
//Print Value
echo "
";";
print_r($obj_h1);
echo "
?>
No comments:
Post a Comment