Archive

Archive for the ‘PHP5 Magic Methods’ Category

PHP5 Tutorial – Magic Methods – __clone() method

November 5th, 2007 admin 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 admin 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 admin 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…

PHP5 Tutorial – Magic Methods – __call() method

November 3rd, 2007 admin 2 comments

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…

PHP5 Tutorial – Magic Methods – __isset() and __unset()

November 3rd, 2007 admin 6 comments

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…

PHP5 OOPS Tutorial __get() and __set()

October 26th, 2007 admin 2 comments

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 Tutorial – Magic Methods – __toString() method

October 25th, 2007 admin 2 comments

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…