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…
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…
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…
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…
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…
This tutorial will teach you how and when to use the magic method __call().
The magic method __call() is to undeclared methods what __get() and __set() are to undeclared data member.
These methods are automatically called internally when the program tires to execute a method that has not been defined within the class at the time of development.
Read more…
In my previous PHP5 OOPS tutorial on PHP5 Tutorial – Magic Methods – __get() and __set(), we learnt how and when to use these magic methods.
This PHP5 OOPS tutorial will teach you how and when to use the magic methods __isset() and __unset().
Read more…
This PHP5 tutorial discusses about PHP arrays.
What is an array?
Generally speaking an array is continuous allocation of memory of similar data types.
This means that you can define a single array variable which can hold multiple values at the same time. These multiple values are retrieved using an index number. Don’t worry if you did not get this… more explanation ahead.
The same definition mentioned above is also applicable for PHP as well, with an exception that it can store data of different data types in one array.
Memory representation of an array

In the above diagram, you can see that variable $a has been allocated 5 slots of memory in which data can be stored. Each of these slots / boxes are referred using an index number that starts from 0. A common mistake for people learning arrays is to consider the starting position of an array as 1.
Read more…
This article talks about the use of __get() (double underscore – get()) and __set() (double underscore – set()) PHP5 OOPS magic methods.
By default PHP is a Loosely typed language and therefore it is not necessary to declare variables before using them. This also holds true for using class members. Look at an example below.
Read more…
PHP5 provides a magic method by the name of __toString() (double underscore followed by toString()) which is useful for debugging purposes.
The __toString() method is automatically called when an object in PHP5 is converted into a string for the purpose of display or concatenation.
Read more…