#native_company# #native_desc#
#native_cta#

Object Oriented Programming in PHP: The Way to Large PHP Projects Page 9

By Luis Argerich
on January 25, 2008

Entering a Danger Zone

When you serialize an object you get a string which has a certain format, you may investigate
it if you are curious, one of the things the string has is the name of the class (nice!)
you may extract it using:

<?php

$herring=serialize($obj);

$vec=explode(':',$herring);

$nam=str_replace(""",'',$vec[2]);

?>



So suposse you create a class “Universe” and force that all classes must extend universe,
you can define a method clone in Universe as:

<?php

class Universe {

    function clone() {

        
$herring=serialize($this);

        
$vec=explode(':',$herring);

        
$nam=str_replace(""",'',$vec[2]);

        
$ret=new $nam;

        return 
$ret;

    }

}

//Then:

$obj=new Something();

    
//Something extends Universe !!

$other=$obj->clone();

?>



What you get is a new object of class Something created the same way as using new, the
constructor is called, etc.
I don’t know if this is useful for you, but the Universe class which knows the name
of the derived class is a nice concept to experiment.
The only limit is your imagination.
Note: I’m using PHP4, something I write here may not work in PHP3.

1
|
2
|
3
|
4
|
5
|
6
|
7
|
8
|
9