Senin, 09 Maret 2015

Object-Oriented PHP for Beginners Part 1

For many PHP programmers, object-oriented programming is a frightening concept, full of complicated syntax and other roadblocks. As detailed in my book, Pro PHP and jQuery, you'll learn the concepts behind object-oriented programming (OOP), a style of coding in which related actions are grouped into classes to aid in creating more-compact, effective code.
Object-oriented programming is a style of coding that allows developers to group similar tasks into classes. This helps keep code following the tenet "don't repeat yourself" (DRY) and easy-to-maintain.
"Object-oriented programming is a style of coding that allows developers to group similar tasks into classes."
One of the major benefits of DRY programming is that, if a piece of information changes in your program, usually only one change is required to update the code. One of the biggest nightmares for developers is maintaining code where data is declared over and over again, meaning any changes to the program become an infinitely more frustrating game of Where's Waldo? as they hunt for duplicated data and functionality.
OOP is intimidating to a lot of developers because it introduces new syntax and, at a glance, appears to be far more complex than simple procedural, or inline, code. However, upon closer inspection, OOP is actually a very straightforward and ultimately simpler approach to programming.
Before you can get too deep into the finer points of OOP, a basic understanding of the differences between objects and classes is necessary. This section will go over the building blocks of classes, their different capabilities, and some of their uses.
Photos by Instant Jefferson and John Wardell
Developers start talking about objects and classes, and they appear to be interchangeable terms. This is not the case, however.
Right off the bat, there's confusion in OOP: seasoned developers start talking about objects and classes, and they appear to be interchangeable terms. This is not the case, however, though the difference can be tough to wrap your head around at first.
A class, for example, is like a blueprint for a house. It defines the shape of the house on paper, with relationships between the different parts of the house clearly defined and planned out, even though the house doesn't exist.
An object, then, is like the actual house built according to that blueprint. The data stored in the object is like the wood, wires, and concrete that compose the house: without being assembled according to the blueprint, it's just a pile of stuff. However, when it all comes together, it becomes an organized, useful house.
Classes form the structure of data and actions and use that information to build objects. More than one object can be built from the same class at the same time, each one independent of the others. Continuing with our construction analogy, it's similar to the way an entire subdivision can be built from the same blueprint: 150 different houses that all look the same but have different
families and decorations inside.
The syntax to create a class is pretty straightforward: declare a class using theclass keyword, followed by the name of the class and a set of curly braces ({}):
After creating the class, a new class can be instantiated and stored in a variable using the new keyword:
To see the contents of the class, use var_dump():
Try out this process by putting all the preceding code in a new file called test.phpin [your local] testing folder:
Load the page in your browser at http://localhost/test.php and the following should display:
In its simplest form, you've just completed your first OOP script.
To add data to a class, properties, or class-specific variables, are used. These work exactly like regular variables, except they're bound to the object and therefore can only be accessed using the object.
To add a property to MyClass, add the following code to your script:
The keyword public determines the visibility of the property, which you'll learn about a little later in this chapter. Next, the property is named using standard variable syntax, and a value is assigned (though class properties do not need an initial value).
To read this property and output it to the browser, reference the object from which to read and the property to be read:
Because multiple instances of a class can exist, if the individual object is not referenced, the script would be unable to determine which object to read from. The use of the arrow (->) is an OOP construct that accesses the contained properties and methods of a given object.
Modify the script in test.php to read out the property rather than dumping the whole class by modifying the code as shown:
Reloading your browser now outputs the following:
Methods are class-specific functions. Individual actions that an object will be able to perform are defined within the class as methods.
For instance, to create methods that would set and get the value of the class property $prop1, add the following to your code:
Note — OOP allows objects to reference themselves using $this. When working within a method, use $this in the same way you would use the object name outside the class.
To use these methods, call them just like regular functions, but first, reference the object they belong to. Read the property from MyClass, change its value, and read it out again by making the modifications below:
Reload your browser, and you'll see the following:
"The power of OOP becomes apparent when using multiple instances of the
same class."
When you load the results in your browser, they read as follows:
As you can see, OOP keeps objects as separate entities, which makes for easy separation of different pieces of code into small, related bundles.

Ref