Saturday, September 6, 2008

OOP using PHP - Chapter 02

0 comments
Save to del.icio.us 0 hits!

In the previews chapter i explaining about of OOP and how to create a basic class and method.
In this chapter we learn of class variables and visibility, constructor method..


Class variables and Visibility


Within classes, we first define our variables. In this case, our first variable is $name. A handy feature of PHP5 is increased visibility control. Our $name variable currently has a visibility of private. Private visibility means that the variable can only be accessed via the methods inside the class (like setName and getName). If we set the variable visibility to public, our variable could be accessed externally (without the methods). The final visibility setting for a variable is protected, which means that variable access is limited to parent and inherited classes, which we will discuss more later.



/* instantiate object */
$cesar = new User();

/* access private variables via methods, then try without methods */
$cesar->setName( 'Cesar Mancilla' );
echo $cesar->getName();
echo $cesar->name; // breaks our code

/* access public variables without methods will work */
$cesar->city = 'California';
echo $cesar->location; //->California

class User
{
private $name; // only can be accessed with methods.

public $city; // can be accessed without methods

function setName( $val )
{
$this->name = $val;
return;
}

function getName()
{
return $this->name;
}
}

So which variable visibility should you choose? The answer is that it depends. In general, you should be fine using private. Private is especially useful because it supports encapsulation - the ability to hide data and only make it accessible through a given interface. In OOP, an interface represents the functionality given to a particular object.


Just as we set a variable's visibility, we can also set visibility for methods. By default, if we don't set the visibility of our methods, they will be set to public. For the sake of time, we will not dig into examples with different method visibility. Continue on to learn about an important and useful method, the constructor.


Note about $this: Within methods, we refer to class variables and other methods using $this->. This can be seen within the setName and getName methods. $this is a default variable created upon instantiation and enables an object to reference itself.


Note about naming conventions: While set[PropertyName] and
get[PropertyName] are popular method names because they are easily understood, they are not required. You can name these methods whatever you want.


Constructor Method


Now is a good time to look at the constructor method. The constructor is a method that will automatically be run when an object is instantiated. In PHP5, we have methods called "Magic Methods" (i.e. __construct, __destruct, and a few others) which begin with two underscores. In PHP4, the constructor method would be given the same name as the class itself. So in PHP4, if we have a class named User, our constructor method would also be named User (not __construct). Remember, you do not need to call the constructor method explicitly because it will be automatically run when an instance of that class is created. Here's a constructor in action:



$cesar = new User();
echo $cesar->getJob(); // Developer

class User
{
private $job;

function __construct()
{
$this->job = 'Developer';
}

/* job methods */
function getJob()
{
return $this->job;
}
}

Now you might be starting to see one of the benefits of OOP - much of the code lies in the background. We could include files containing our classes to hide the heavy duty code even more. Because the naming conventions of classes and methods are fairly self explanatory, we won't need to constantly look back at our included files.

Thursday, September 4, 2008

OOP using PHP - Part 1

0 comments
Save to del.icio.us 0 hits!

This tutorial is an introduction to object oriented programming (OOP) with PHP. Upon finishing this tutorial, you should have the basic tools necessary to begin navigating the world of OOP. Throughout, we will build upon one example while key terms and nuances to OOP with PHP are introduced. In addition, some time will be spent showing how to integrate object oriented PHP with MySQL.

Creating Our First Object

Not surprisingly, object oriented programming is focused around objects. While the idea of objects may be foreign to you in coding terms, understanding what objects are and why we use them shouldn't take long. In short, our non-programming world is comprised of objects. Computers, fish, clouds, people, and cars are all objects. Objects have properties like color, size, name, and speed. Objects can also be comprised of other objects: cars have doors; doors have handles; handles have plastic levers; so on and so forth. Objects are everywhere around us. For this reason, many programmers find object oriented programming relatively easy to understand.

So how do objects translate into code? If we were building a website about users, we would create one or many user objects. Objects are created using classes. Classes are groups of related variables and functions. Variables hold our object's properties like color, size, and speed. Functions perform actions like set variable values or open files. In this example, we could create user objects with a User class. You can think of the User class as a template for any user objects.

Creating an object is called instantiation (creating an instance). Let's instantiate a user object and code the corresponding User class:



/* create new object */
$cesar = new User();

/* class to create objects with */
class User
{
  //Methods
}

Right now this user can't do anything and doesn't have any attributes. Within classes, we use methods (aka functions) and variables to give our objects functionality and properties. Let's redo our first example so that our users can have a name:



/* create a new object */
$cesar = new User();

/* call object methods */
$cesar->setName( 'Cesar Mancilla' );
echo $cesar->getName();

class User
{
  private $name;

  public function setName( $val )
  {
    $this->name = $val;
    return;
  }

  public function getName()
  {
    return $this->name;
  }
}

As you can see, methods are declared using function [methodName] format, as is standard in PHP. This simple example shows us how we can apply a name to the object, then access the name.

About Me

Subscribe now!Feeds RSS

Latest posts

Hot Links

Join into my community

Followers