To see what you can do with object-oriented PHP, take a look at the huge range of PHP scripts on CodeCanyon, such as this SQLite Object-Oriented Framework.
Or, if you're struggling with the do-it-yourself approach, you could hire a professional on Envato Studio either to fix errors for you or to create full PHP applications and modules.
Understanding Object-Oriented Programming
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.
Understanding Objects and Classes
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.Recognizing the Differences Between Objects and Classes
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.
Structuring Classes
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 ({}
):
<?php
class
MyClass
{
// Class properties and methods go here
}
?>
new
keyword:
$obj
=
new
MyClass;
var_dump()
:
var_dump(
$obj
);
test.php
in [your local] testing folder:
<?php
class
MyClass
{
// Class properties and methods go here
}
$obj
=
new
MyClass;
var_dump(
$obj
);
?>
http://localhost/test.php
and the following should display:
object(MyClass)#1 (0) { }
Next Steap
Post a Comment
Silahkan anda tulis komentar di bawah ini !