PHP Object Oriented Programming OOP for Beginners
Taking a Logical Look at Using OOP in Your PHP Applications
To start, we'll take a look at what makes up an object.
Objects in PHP are instantiated (created, used) by making a call to a class.
$user = new User();
In this case, $user becomes the object, and the class User is used as a template. More on this below.
The class being called contains variables, which are referred to as properties in the world of OOP. The class also contains functions, referred to as methods in OOP.
In this case, we aren't telling PHP to actually create a new user. We are simply telling PHP that the variable $user will contain the variables (properties) and functions (methods) which are defined in the actual class. Typically, each class is stored in it's own file to increase modularity. In this case, the user class may be stored in lib/User.class.php. The code to instantiate (create, use) an object of this class would look like the following.
require_once('lib/User.class.php');
$user = new User();
So, basically, the class is the template and the object is one instance of this template. In this case, User.class.php is the class and $user is the object. Before we go any further, let's take a look at what this class might look like. Note that what immediately follows the word class is the class name and is used to instantiate (create, use) an object of this class.
class User
{
var $id;
var $name;
var $address;
function setName($name)
{
$this->name = $name;
}
}
Right now you're probably saying to yourself, what is $this? I don't see it defined anywhere. $this in PHP is used to refer to the current object (current instance of that class). The following example will clarify this further.
require_once('lib/User.class.php');
$user = new User();
$user->setName('Joe Smith');
What we've done here is said $user will contain $id, $name and $address (the properties). Next, we call the function (method) setname. We do this by using an arrow (->) which as you can see is a dash followed by a greater than sign. Think of this as pointing to the function (method) in the class. This is also true of accessing variables (properties) of an object. At this point, the function (method) within the class is just like any other function. You can pass in information in just like any other function and also have information returned, just like any other function. The setName function (method) then takes what was passed in and sets $this->name equal to $name, which was passed in. In this case, we passed in the string Joe Smith. Using $this->name in the method, effectively defined $user->name to the string Joe Smith. $user->name being the correct way to reference variables (properties) of an object, outside of the class.
require_once('lib/User.class.php');
$user = new User();
$user->setName('Joe Smith');
echo $user->name;
This would echo the string Joe Smith, because we defined $user->name by using the setName method. $this is used in the class because the class has no way of knowing that you have instantiated (created, used) $user to create the object. Another example below.
require_once('lib/User.class.php');
$user = new User();
$user2 = new User();
$user->setName('Joe Smith');
$user2->setName('John Henry');
echo $user->name;
echo $user2->name;
In this case, first Joe Smith would be echoed, and then John Henry would be echoed. As you can see, we used the same method setName to define the values of two different variables (properties). The method (function) used $this in place of $user and $user2.
A class can be thought of as a template for some kind of information, or sometimes as template for a particular process. Usually, when dealing with business logic (databases, their tables and the rows of the tables) a class can be thought of as the table. You would then define a property (variable) for each column of the table in the class. Objects could then be thought of as a row in the table, the actual information. The class then goes a step further by defining methods (functions) which are used to access and change the information. A more practical user class an be found below. Note, this is just an example and by no means necessarily follows best practices.
class User
{
var $id;
var $name;
var $address;
function setName($name)
{
$this->name = $name;
}
function setAddress($address)
{
$this->address = $address;
}
function saveUser()
{
myql_query("INSERT INTO user (name, address) VALUES ('$this->name', '$this->address');
}
}
In this case, we could use the following code to create an entry in the user table.
$user = new User();
$user->setName('Joe Smith');
$user->setAddress('555 5th St');
$user->saveUser();
This brings to light one of the most basic and powerful features of object oriented programming, which is abstraction. Abstraction simplifies programming by providing a simple interface to work with a class. In this case, you only need to know $user->saveUser(), you don't need to know how to use the mysql_query command.
This is particularly powerful when your class knows how to deal with more than one database. You can then easily change the database you are using by making changes to the class rather than changing each individual query command in your program.
This should be enough to get you started and hopefully get you closer to an object oriented mindset. A short summary will follow.
Class - Template for an object. Can be thought of as a database table. Contains properties, which are variables. Contains methods, which are functions.
Object - Instance of a class. Allows you to access properties (variables) as well as methods (functions) of the class.
Instantiate - Create an object which inherits the properties (variables) and methods (functions) of a class (like $user in our example).
$this - Refers to the current object WITHIN the class. Allows your class methods (functions) to interact with instantiated objects (like $user in our example).
-> - Arrow created with a dash followed by a greater than sign. "Points" to the properties (variables) or methods (functions) within an object.
Published by jeff griffiths
Computer Programmer interested in cutting edge web development techniques and best practices. View profile
- Object Oriented Programming - The Growing NeedIn today's competitive world, the need of the hour is to develop simple and speedy solutions that can be instantly applied to varied requirements.
- Computer Programming - Then and NowDescribes the history of computer programming language. The job of the computer programmer, and future career prospects.
- Using Java Access Modifiers in Java ProgrammingJava Access Modifiers (also known as Visibility Modifiers) regulate access to classes, fields and methods in Java. These modifiers determine whether a field or method in a class, can be used or invoked by another meth...
- How to Choose the Right Programming LanguageSo you've decided you want to become a programmer, first step is decide in what programming language you want to program in.
- How to Code in XNA for BeginnersA comprehensive guide to learning the basics of the XNA API and to get started programming in XNA.
- Class Reunion Planning: Reunion Committee
- Class Reunion Planning: Locating Classmates
- BlurtIt.com's "Questions and Answers" Page Offers Free Answers to User Questions o...
- T-Mobile Sidekick III Actual User Review
- Why You Should Never Sign Up as an Authorized User on a Credit Card
- Features of Object Oriented Programming
- Advantages of Object-Oriented Programming

1 Comments
Post a CommentYou've done a superb job on explaining the basics of OOP and in guiding the reader into taking the first steps. Thumbs up!