หน้าเว็บ

วันจันทร์ที่ 28 กุมภาพันธ์ พ.ศ. 2554

PHP5 OOP

    When you begin a new project, one of the first things you have to consider is the structure of your
code. Whether you’re coding something as simple as an online contact form, or as complex as a
full-featured content management system, how you organize your code is going to influence the
performance and maintainability of the end product.

    When you use a language like PHP, there are two main routes you can go: procedural programming
and object-oriented programming—OOP for short. Each strategy has its own benefits and
limitations.

Procedural Programming versus OOP
Procedural programming often emphasizes writing code that is as concise as possible and coding
directly for the end result. In other words, most procedural programming uses targeted groups
of functions that immediately address the problem at hand—usually nothing more, and nothing
less. In most situations, this gets you extremely efficient and high-performance applications. One
of the downsides to this approach is a lack of maintainability. If the project grows large enough,
the developer or developers could end up having to maintain a large number of individual functions,
and in some cases, the logic of different functions can become confusingly similar.
Object-oriented programming (OOP), on the other hand, emphasizes abstract relationships and a
hierarchy of related functionality. Similar functionality can all share a common core, making maintenance
much easier. Code reuse is increased as well, as you can easily adapt the abstracted base
functionality for new tasks. OOP also can aid in large-scale program design, helping encapsulate
and categorize the different sets of functionality required by each part of the system. Such organization
and modularity can come at a price, however. If your object-oriented system is poorly
designed, it can actually be harder to maintain than any of the alternatives. Often, the extreme
modularity and “code-heaviness” of object-oriented designs can suffer from poor performance.

   Once you get past the problems caused by poor object-oriented design, you will find that creating a system
using a custom set of PHP objects, or even a full-blown API, can yield benefits that most every
developer will appreciate. With that, you can now begin to take a look at how PHP5 implements objectoriented
programming.

Basic Class Definitions
The basic unit of code in object-oriented PHP is the class. Simply put, a class is a way to encapsulate
related functionality and data in one entity. This encapsulation can be used to hide internal operations
from external code, and helps simplify the external interaction with the data. A class is a formal description
of a grouping of code, a programmatic recipe if you will. A class by itself, like a recipe, is merely a
cluster of instructions, and not something that can directly be used—you don’t eat the actual recipe, do
you? To use classes, you will create an instance of the class, called an object—similar to using the recipe
to prepare a dish you can actually eat. Classes define the properties and actions of a group of code, and
objects are individual instances of that set of commands.
An easy way to understand classes is to relate class code to physical objects. Many times, classes
would represent these real-world objects. You might have a class named Car that has a property called
occupants, which might keep track of the number of people in the car. It might even contain a method
called brake(), which would perform its similarly-named task. Like many real world items, classes
have a combination of attributes that describe the individual object, called properties in OOP, and a set of
actions that they can perform, which are called methods in the object-oriented world.

From: Profresstional LAMP

ไม่มีความคิดเห็น:

แสดงความคิดเห็น

If you want an answer, you have to ask....