OOP was designed to allow programmers to more easily model their programs upon real world scenarios - programmers literally define things (objects) in their world (program), set a few basic properties, then ask them to do things.
The basic elements of object-oriented programs are objects. It’s easiest to understand objects as physical objects. For example, a car is an object. A car has properties, such as color, model, engine, and tires, also called attributes. A car has things it can do, too, such as move forward, move backward, park, roll over.
In general, objects are nouns. A person is an object. However, objects are not just physical objects. Often objects, like nouns, are more conceptual. For example, a bank account is not something we can hold in our hand, but it can be considered an object. A file is often an object. E-mail messages, addresses, songs, TV shows, meetings, and dates can all be objects.
A class is the script that serves as the template, or the pattern, that is used to create an object. The class defines the properties, the attributes, of the object. It also defines the things the object can do — its responsibilities. For example, we write a class that defines a car as four wheels and an engine and lists the things it can do, such as move forward and park. Then, given that class, we can write a statement that creates a car object. Our new car is created following the pattern in our class. When we use our car object, we may find that it is missing a few important things, like a door or a steering wheel or a reverse gear. That’s because we left those out of the class when we wrote it.
A class is a package containing two things: data and methods to access and modify that data. The data portion consists of variables; they're known as properties. The other part of a class is a set of functions that can alter a class properties; they're called methods.
As the person who writes a class, we know how things work inside the class. But it’s not necessary to know how an object accomplishes its responsibilities in order to use it; anyone can use a class. We have no clue how a telephone object works, but we can use it to make a phone call. The person who built the telephone knows what’s happening inside it. When there’s new technology, the phone builder can open the phone and improve it. As long as he doesn’t change the interface — the keypad and buttons — it doesn’t affect our use of the phone at all.
When we define a class, we don't define an object that can be accessed and manipulated. Instead, we define a template for an object. From this blueprint, we create malleable objects through a process known as instantiation.
A program can have multiple objects of the same class, just as a person can have more than one book or many pieces of fruit. Classes also live in a defined hierarchy. At the top of the chain, there is a generic class. Each class down the line is more specialized than its parent. For example, a parent class could be a building. Buildings can be further divided into residential and commercial. Residential buildings can be further subdivided into houses and apartment buildings, and so forth. Both houses and apartment buildings have the same set of properties as all residential buildings, just as residential and commercial buildings share some things in common.
When classes are used to express these parent-child relationships, the child class inherits the properties and methods defined in the parent class. This allows you to reuse the code from the parent class and requires you to write code only to adapt the new child to its specialized circumstances. This is called inheritance and is one of the major advantages of classes over functions. The process of defining a child class from a parent is known as subclassing or extending.
Classes in PHP are easy to define and create:
Class Demo
{
Function sayHello($name)
{
Print " Hello $name.";
}
}
The class keyword defines a class, just as function defines a function. Method declaration is identical to how functions are defined.
Creating an object is as follows.
$objDemo= new Demo;
The new keyword instantiates an object $objDemo from Demo class.
No comments:
Post a Comment