Archive

Archive for the ‘PHP Tutorials’ Category

PHP5 Tutorial OOPS – Creating a PHP5 Destructor __destruct()

November 14th, 2007 2 comments

Definition of a Destructor
A destructor is a special function of a class that is automatically executed whenever an object of a class is destroyed.

What does this all mean?
Let’s revisit that definition in more simple terms. A destructor is a special function – this means that a destructor is a function; but its special. But, why is it special? It’s special because it is automatically executed or called when an object of a class is destroyed. An object of a class is destroyed when

  1. it goes out of scope,
  2. when you specifically set it to null,
  3. when you unset it or when the program execution is over.

Read more…

Categories: PHP, PHP Tutorials, PHP5 OOPS Tutorials Tags:

PHP Class Constructor with Examples

November 14th, 2007 10 comments

Definition of a Constructor
A constructor is a special function of a class that is automatically executed whenever an object of a class gets instantiated.

What does this all mean?
Lets revisit that definition in more simple terms. A constructor is a special function – this means that a constructor is a function; but its special. But, why is it special? It’s special because it is automatically executed or called when an object of a class is created.

Read more…

Categories: PHP, PHP Tutorials, PHP5 OOPS Tutorials Tags:

PHP5 Tutorial – Defining Methods of a PHP5 Class

November 8th, 2007 8 comments

In this PHP5 tutorial you will learn about methods and how to declare and use them in PHP5 class.

Definition of an class method
A class method/functions is the behavior/functionality of a class i.e. they provide the necessary code for the class in which it is defined. Examples could be a saveCustomer() method in the class Customer or a printDocument() in the Document class.

Methods act (perform operations) on the data members of the class and can be declared as private or public. A class method is exactly similar to PHP functions, it’s just that class functions are declared inside classes and accessed using the -> (arrow operator / dereferencing operator).

Read more…

PHP5 Tutorial – Defining Attributes of a PHP5 Class

November 8th, 2007 8 comments

In this tutorial you will learn about class attributes and how to declare & use them in PHP5 classes.

Definition of an class attribute
An attribute is also know as data members and is used to hold data of a class. The data that it holds are specific to the nature of the class in which it has been defined. For example, a Customer class would hold data related to a customer, an Order class would hold data related a an order.

Read more…

Categories: PHP, PHP Tutorials, PHP5 OOPS Tutorials Tags:

PHP5 Tutorial – Learn to Create a PHP5 Class Object

November 8th, 2007 1 comment

In the earlier PHP5 OOPS tutorial you learnt how to create a class in PHP5. In this tutorial you will learn how to create an object of a PHP5 class. But before we begin, lets understand what is an object.

Definition of an Object
An object is a living instance of a class. This means that an object is created from the definition of the class and is loaded in memory. A good analogy to understand this is to compare objects with humans – and understand that all of us (you and I) are objects. If God wants to send a human to earth, what is easy for Him to do? Create and define properties and attributes of each human separately or create a one time template and generate objects out if it. Therefore, this onetime template is a Class and you, I & everyone in this world is an object – that is a living instance of class Human.

Read more…

Categories: PHP, PHP Tutorials, PHP5 OOPS Tutorials Tags:

PHP5 Tutorial – Learn to create a PHP5 Class

November 8th, 2007 8 comments

Before we begin learning how to create PHP5 Class, lets first understand the meaning of a class in object oriented programming practices.

Definition of a Class


A class is user defined data type that contains attributes or data members; and methods which work on the data members. (You will learn more about data members and methods in following tutorials. This tutorial focuses only on learning how to create a Class in PHP5)

To create a class, you need to use the keyword class followed by the name of the class. The name of the class should be meaningful to exist within the system (See note on naming a class towards the end of the article). The body of the class is placed between two curly brackets within which you declare class data members/variables and class methods.

Read more…

Categories: PHP, PHP Tutorials, PHP5 OOPS Tutorials Tags:

PHP5 OOPS Tutorial – Introduction to PHP5 OOPS Features

November 7th, 2007 12 comments

PHP5 has made a lot of improvements as regarding OOPS is concerned. Although it has not been up to the mark with the likes of Java, .NET and C++; but when compared to PHP4, PHP5 has made significant improvements.

In this post, I propose to cover the following topics:

  • About PHP5
  • Future of PHP
  • New keywords in PHP5
  • A note on Garbage Collection
  • Naming Conventions
  • Note on E_STRICT
  • Built in Classes (SPL)

Read more…

Categories: PHP, PHP Tutorials, PHP5 OOPS Tutorials Tags:

PHP5 Tutorial – Magic Methods – __clone() method

November 5th, 2007 26 comments

Before I begin to explain the use of a __clone() method, lets try and understand what does object cloning mean.

To clone an object means to create a duplicate of an object. With regular variables $a = $b means that a new variable $a gets created that contains the value of $b. This means that 2 variables get created.

With objects $obj2 = $obj1 does not mean that a new object i.e. $obj2 gets created. When we execute $obj2 = $obj1, the reference of $obj1 is assigned to $obj2. This means that $obj1 and $obj2 point to the same memory space. Look at the diagram below.

Read more…

PHP5 Tutorial – Magic Methods – __sleep() and __wakeup()

November 3rd, 2007 1 comment

The magic method __sleep() and __wakeup() is called when an object is serialized. The magic method __sleep() and __wakeup() provides a method to clean up and restore objects before being serialized.

Working with the magic method __sleep()

__sleep() magic method is called when the object of a class is about to be serialized. This magic method __sleep() does not accept any parameter and returns an array. The array should contain a list of class members that should be serialized. This means that if you don’t wish to serialize a particular class member, you should not include it in the array. Look at the example below:

Read more…

PHP5 Tutorial – Magic Methods – __autoload() method

November 3rd, 2007 4 comments

This tutorial will teach you how and when to use the magic method __autoload().

The magic method __autoload() function is a convenience that allows you to use classes without having to explicitly write code to include them.

The magic method __autoload() is not included in your class definition as this is to be called once in a script. The best place to put the autoload() file is in your configuration file which is loaded in all your other scripts.
Read more…