Featured Posts

Writing Website Scrapers in PHPWriting Website Scrapers in PHP This article discusses about how to write a website scraper using PHP for web site data extraction. The concepts taught can be applied and programmed in Java, C#, etc. Basically any language that has a...

Readmore

12 common programming mistakes to avoid12 common programming mistakes to avoid Programming is an art and science and like all art and science the only way to learn is from mistakes. I have made many... and I would like to share with you the mistakes that I have made over my journey...

Readmore

7 habits of highly effective freelance programmers7 habits of highly effective freelance programmers I have developed these based on my freelancing experience. Though I have discontinued freelancing, but would like to share my practices with you. These are basic practices and have been developed over...

Readmore

  • Prev
  • Next

PHP5 Tutorial OOPS - Creating a PHP5 Destructor __destruct()

Posted on : 14-11-2007 | By : admin | In : PHP, PHP Tutorials, PHP5 OOPS Tutorials

2

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.

PHP5 Tutorial - Creating a PHP5 Constructor __construct()

Posted on : 14-11-2007 | By : admin | In : PHP, PHP Tutorials, PHP5 OOPS Tutorials

9

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.

PHP5 Tutorial - Defining Methods of a PHP5 Class

Posted on : 08-11-2007 | By : admin | In : PHP, PHP Tutorials, PHP5 OOPS Tutorials

8

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).

PHP5 Tutorial - Defining Attributes of a PHP5 Class

Posted on : 08-11-2007 | By : admin | In : PHP, PHP Tutorials, PHP5 OOPS Tutorials

11

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.

PHP5 Tutorial - Learn to Create a PHP5 Class Object

Posted on : 08-11-2007 | By : admin | In : PHP, PHP Tutorials, PHP5 OOPS Tutorials

2

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.

PHP5 Tutorial - Learn to create a PHP5 Class

Posted on : 08-11-2007 | By : admin | In : PHP, PHP Tutorials, PHP5 OOPS Tutorials

11

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.

PHP5 OOPS Tutorial - Introduction to PHP5 OOPS Features

Posted on : 07-11-2007 | By : admin | In : PHP, PHP Tutorials, PHP5 OOPS Tutorials

12

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)

PHP5 Tutorial - Magic Methods - __clone() method

Posted on : 05-11-2007 | By : admin | In : PHP, PHP Tutorials, PHP5 Magic Methods, PHP5 OOPS Tutorials

25

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.

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

Posted on : 03-11-2007 | By : admin | In : PHP, PHP Tutorials, PHP5 Magic Methods, PHP5 OOPS Tutorials

3

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:

PHP5 Tutorial - Magic Methods - __autoload() method

Posted on : 03-11-2007 | By : admin | In : PHP, PHP Tutorials, PHP5 Magic Methods, PHP5 OOPS Tutorials

6

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.