[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
ObjectOrientedProgrammingInPHP

Objectoriented Programming in PHP

A beginners Guide to Classes and Objects

-------------------------------------------------------------------

This is a beginners guide to object oriented programming in PHP. It covers basic concepts of oop and how to use it with PHP.

OOP Basics

Object Oriented programming exists since the late 70s, when the first complete object oriented programming language, Smalltalk, was released.

The object based concept of Smalltalk was unique at that time and it is even today, though other oop languages like C++ or Java exist. The only language or programming framework, that comes close to that of Smalltalk is the .NET framework. However, Smalltalk remains the only 100 % object oriented programming language, since even controll structures like if, ifelse and while are implemented as classes.

Classes, Objects and Methods

Like any other oop language, the objects modell of PHP supports classes, objects and methods. In PHP, classes define abstract modells of real-world objects like cars, houses, users etc. An object is a representation of a class. Methods are functions that manipulate objects.

The following code shows how to define a class in PHP:


<?

 //defining a class Header 
class Header
{
   //defining 2 variables of type string
  var $h1;              
  var $h2
  //defining a method 
  function printHeader()
   {
     echo this->h1 . " <br />\n";
     echo this->h2 . ""; 
   }
}

?>



In this example, I have defined a class Header, that represents a Header of an HTML document. This class contains 3 members. 2 variables of type string and 1 one method called printHeader().

If you look close at the printHeader() method, you will notice the -> operator. This operator is used to access class member variables. Note that, when using the -> operator you dont need to add the $ dollar sign before the variable. This concept is similar to that of C++, where you use this to access the current object of a class.

Creating an object of a Class

Once we have defined our class, we can create objects of it and manipulate them.

 //Creating a new object of class Header 
$homepageheader = new Header;
$homepageheader->h1 = "<h1>Welcome</h1>";
$homepageheader->h2 = "<h2>This is My Homepage</h2>";
$homepageheader->printHeader();


New objects of classes are defined with the new operator. This concept is similar to that of C++ too. The PHP processor generates a new object and copies it into memory. Theny, you can use this object, which is $homepageheader in the example above, to manipulate it.

Objects of classes can be used like any other variable in PHP. You can assign values, print it, manipulate it etc.

Creating objects with Constructors

In the example above, I have simply declared a new varibale named $homepageheader to create a new object. However, this is not the only way to create objects. If you are familiar with C++, you know that each class has an implicit standard constructor which has the same name as the class and no return value.

The same goes for PHP. You can declare a constructor that must have the same name of the class.


<?

 //defining a class Header 
class Header
{
   //defining 2 variables of type string
  var $h1;              
  var $h2
  //defining a constructor for class Header 
 function Header($inputh1, $inputh2)
  {
    this->h1 = $inputh1;
    this->h2 = $inputh2;
  }
  //defining a method 
  function printHeader()
   {
     echo this->h1 . " <br />\n";
     echo this->h2 . ""; 
   }
}

?>



Having defined the constructor, creating the object is nothing more than assigning the right parameters to the constructor.

$homepageheader = new Header("<h1>Welcome</h1>,<h2>This is MyHomepage</h2>");
$homepageheader->printHeader();


This safes not only one line of code, its also a better style of programming.

You may wonder if PHP also supports Destructors since i always draw a relation to C++. To make it short, PHP does not support Destructors . In contrast to C++, where you are responsible for capturing and releasing ressources this is not important in PHP.

last edited (June 2, 2004) by athomas, Number of views: 7574, Current Rev: 12 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.