Quakenet/#php Tutorial

Note: If you opened this page from an external URL pay attention that all chapters are linked together. Be sure you also read all prior chapters of this tutorial, otherwise you will miss relevant content explained before.

Use of classes

  1. What is OOP?
  2. Create an object
  3. Work with an object
  4. Constructors
  5. Destructors

1. What is OOP?

OOP is the abbrevation for object-oriented programming and is a programming paradigm which programm code can be group together and creates a unit. This programm code is called a class which gets a name. From this class your can create an object. The simpliest description of an object is: an array with functions. Although this description doesn't mention everything about classes and objects its a easy way to understand what an objects is. Objects got some fields like an array got fields and they got functions (called methods) which deals with them. This knowledge is enough to work with classes and objects. If you want to create your own classes you will see the whole scope of classes like information hiding, inheritance, design pattern, ...

2. Create an object

To create an object from a class you must use the keyword new. It is followed by the name of the class.

<?php
$obj 
= new DateTime();
?>

This code creates a new DateTime object and saves inside the $obj variable a link to this created object. The variable doesn't save the object itself only a link to the object. We can check the object with var_dump.

<?php
$obj 
= new DateTime();
var_dump($obj);
?>

The output looks like this.

object(DateTime)#1 (0) {
}

If this object have public attributes they are shown like var_dump is used on an array. But the class DateTime has no public attributes.

3. Work with an object

If you have create an object you want to use it. This can be done with the -> operator. On the left is the variable which points to the object, on the right is the method or attribute which you want to call/access. As an example we call the format() method (date_format).

<?php
$obj 
= new DateTime();
echo 
$obj->format('H:i:s');
?>

Of course we could use date or strftime but we want to learn how to use classes and objects.

4. Constructors

If you create an object a special method is executed, the so called constructor. This method fills the internal attributes with default values. For the DateTime object it saves the timezone and the current time inside the object. Constructors are like normal methods and can have parameters. Yoou can check the function description for date_create to see what parameters are possible for the DateTime class.

<?php
$obj 
= new DateTime("-1 day");
?>

5. Destructors

Destructors are methods which are called by the php engine if the last reference/link to the object is deleted. As there is no possibility to access the object anymore there is no reason to hold it in the memory. PHP calls in this case the destructor which can do some shutdown statements like closing the connection to a database. After that the object is erased from the memory.

Questions about the chapter

No questions

Back to Next to
Copyright © to the OPs of #php/QuakeNet Valid XHTML 1.0 Strict Valid CSS!