We All Have to Start Somewhere
Rather than assume any given level of experience with OOP, I am going to start with some simple OOP principles. I’ve made videos of the materials in this post: 1) Creating a class and 2) Calling a class. I’m working on adding a video page to the Sandlight page, but for now you can see the videos in the Youtube site.
Object Oriented Programming (OOP) is for organizing your thinking about programming. It will not make your programs run faster, but if you’ve developed algorithms to help speed up your PHP programs, you can use the same algorithms in OOP. You can see the main value of OOP when your begin developing larger programs. A good understanding of OOP will help you program faster, repair and/or change programs faster. In the very real world where developers are paid to develop programs in PHP, clients want you to make changes, and as your program grows, making changes is more difficult. OOP and Design Patterns will save your life in these situations.
Objects
The first thing to understand is that programs are developed in modules rather than in long single programs stored in a single file. If you hear someone brag about a 3,000 line program, let’s hope those 3,000 lines are organized into several classes that encapsulate different tasks. Then, let’s hope that when Elmo changes one line of code, the whole thing doesn’t come crashing down on his head.
So how do we make objects? In PHP (and other languages), you begin with a class. Generally, when you create a class, you put together an object that does one thing. For example, the following class does one thing: It prints out “Hello World.” (Save the following class as class1.php
)
class FirstClass
{
private $prop;
public function firstMeth()
{
$this->prop="Hello World";
echo $this->prop;
}
}
?>
|
It’s made up of a property ($prop) and a method (firstMeth()). In general, a property is a characteristic of the object (like a person has brown eyes), and the method is something that the object does (like a person writes programs.) This class has a property with a value of “Hello World” and a method that prints out the contents of the property. Don’t worry about what private and public mean; we’ll get to those in another post. (Click to learn how objects communicate!)
Class Communication
The idea behind OOP and Design Patterns is that the different classes communicate with one another. If you want something done, you make a request to the appropriate object. In design patterns, the requesting class is the “client”—it makes requests. So, we’ll just use the class name “Client” for those objects that make the initial request. Once the initial request is made to a class, it may have to communicate with and make requests from other classes to get everything required in an initial request. For example, the following class is a client that makes a request to the initial class: (Save the following class as Client1.php
).
include_once("class1.php");
class Client
{
private $caller;
public function callFirst()
{
$this->caller=new FirstClass();
$this->caller->firstMeth();
}
}
$worker=new Client();
$worker->callFirst();
?>
|
At the bottom of the listing a $worker variable instantiates the Client class. By doing so, it becomes a Client object and may use the object’s public methods. That method calls the first class (conveniently named, FirstClass) and it calls its public method, firstMeth(). By making the call, you will see “Hello World” printed to the screen, and your day is made!
What you should take away from this is nothing more than programming can be accomplished by objects communicating with one another. Objects have properties and/or methods that perform tasks in a neat and orderly manner. We’ll have more introductory OOP materials and reply to any comments you have to help you get rolling.
Copyright © 2013 William Sanders. All Rights Reserved.
0 Responses to “PHP OOP: An Introduction to Rocket Science”